amdgpu_cs.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. /*
  2. * Copyright 2008 Jerome Glisse.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors:
  25. * Jerome Glisse <glisse@freedesktop.org>
  26. */
  27. #include <linux/list_sort.h>
  28. #include <drm/drmP.h>
  29. #include <drm/amdgpu_drm.h>
  30. #include "amdgpu.h"
  31. #include "amdgpu_trace.h"
  32. #define AMDGPU_CS_MAX_PRIORITY 32u
  33. #define AMDGPU_CS_NUM_BUCKETS (AMDGPU_CS_MAX_PRIORITY + 1)
  34. /* This is based on the bucket sort with O(n) time complexity.
  35. * An item with priority "i" is added to bucket[i]. The lists are then
  36. * concatenated in descending order.
  37. */
  38. struct amdgpu_cs_buckets {
  39. struct list_head bucket[AMDGPU_CS_NUM_BUCKETS];
  40. };
  41. static void amdgpu_cs_buckets_init(struct amdgpu_cs_buckets *b)
  42. {
  43. unsigned i;
  44. for (i = 0; i < AMDGPU_CS_NUM_BUCKETS; i++)
  45. INIT_LIST_HEAD(&b->bucket[i]);
  46. }
  47. static void amdgpu_cs_buckets_add(struct amdgpu_cs_buckets *b,
  48. struct list_head *item, unsigned priority)
  49. {
  50. /* Since buffers which appear sooner in the relocation list are
  51. * likely to be used more often than buffers which appear later
  52. * in the list, the sort mustn't change the ordering of buffers
  53. * with the same priority, i.e. it must be stable.
  54. */
  55. list_add_tail(item, &b->bucket[min(priority, AMDGPU_CS_MAX_PRIORITY)]);
  56. }
  57. static void amdgpu_cs_buckets_get_list(struct amdgpu_cs_buckets *b,
  58. struct list_head *out_list)
  59. {
  60. unsigned i;
  61. /* Connect the sorted buckets in the output list. */
  62. for (i = 0; i < AMDGPU_CS_NUM_BUCKETS; i++) {
  63. list_splice(&b->bucket[i], out_list);
  64. }
  65. }
  66. int amdgpu_cs_get_ring(struct amdgpu_device *adev, u32 ip_type,
  67. u32 ip_instance, u32 ring,
  68. struct amdgpu_ring **out_ring)
  69. {
  70. /* Right now all IPs have only one instance - multiple rings. */
  71. if (ip_instance != 0) {
  72. DRM_ERROR("invalid ip instance: %d\n", ip_instance);
  73. return -EINVAL;
  74. }
  75. switch (ip_type) {
  76. default:
  77. DRM_ERROR("unknown ip type: %d\n", ip_type);
  78. return -EINVAL;
  79. case AMDGPU_HW_IP_GFX:
  80. if (ring < adev->gfx.num_gfx_rings) {
  81. *out_ring = &adev->gfx.gfx_ring[ring];
  82. } else {
  83. DRM_ERROR("only %d gfx rings are supported now\n",
  84. adev->gfx.num_gfx_rings);
  85. return -EINVAL;
  86. }
  87. break;
  88. case AMDGPU_HW_IP_COMPUTE:
  89. if (ring < adev->gfx.num_compute_rings) {
  90. *out_ring = &adev->gfx.compute_ring[ring];
  91. } else {
  92. DRM_ERROR("only %d compute rings are supported now\n",
  93. adev->gfx.num_compute_rings);
  94. return -EINVAL;
  95. }
  96. break;
  97. case AMDGPU_HW_IP_DMA:
  98. if (ring < 2) {
  99. *out_ring = &adev->sdma[ring].ring;
  100. } else {
  101. DRM_ERROR("only two SDMA rings are supported\n");
  102. return -EINVAL;
  103. }
  104. break;
  105. case AMDGPU_HW_IP_UVD:
  106. *out_ring = &adev->uvd.ring;
  107. break;
  108. case AMDGPU_HW_IP_VCE:
  109. if (ring < 2){
  110. *out_ring = &adev->vce.ring[ring];
  111. } else {
  112. DRM_ERROR("only two VCE rings are supported\n");
  113. return -EINVAL;
  114. }
  115. break;
  116. }
  117. return 0;
  118. }
  119. int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, void *data)
  120. {
  121. union drm_amdgpu_cs *cs = data;
  122. uint64_t *chunk_array_user;
  123. uint64_t *chunk_array = NULL;
  124. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  125. unsigned size, i;
  126. int r = 0;
  127. if (!cs->in.num_chunks)
  128. goto out;
  129. p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id);
  130. if (!p->ctx) {
  131. r = -EINVAL;
  132. goto out;
  133. }
  134. p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
  135. /* get chunks */
  136. INIT_LIST_HEAD(&p->validated);
  137. chunk_array = kcalloc(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL);
  138. if (chunk_array == NULL) {
  139. r = -ENOMEM;
  140. goto out;
  141. }
  142. chunk_array_user = (uint64_t *)(unsigned long)(cs->in.chunks);
  143. if (copy_from_user(chunk_array, chunk_array_user,
  144. sizeof(uint64_t)*cs->in.num_chunks)) {
  145. r = -EFAULT;
  146. goto out;
  147. }
  148. p->nchunks = cs->in.num_chunks;
  149. p->chunks = kcalloc(p->nchunks, sizeof(struct amdgpu_cs_chunk),
  150. GFP_KERNEL);
  151. if (p->chunks == NULL) {
  152. r = -ENOMEM;
  153. goto out;
  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. r = -EFAULT;
  163. goto out;
  164. }
  165. p->chunks[i].chunk_id = user_chunk.chunk_id;
  166. p->chunks[i].length_dw = user_chunk.length_dw;
  167. size = p->chunks[i].length_dw;
  168. cdata = (void __user *)(unsigned long)user_chunk.chunk_data;
  169. p->chunks[i].user_ptr = cdata;
  170. p->chunks[i].kdata = drm_malloc_ab(size, sizeof(uint32_t));
  171. if (p->chunks[i].kdata == NULL) {
  172. r = -ENOMEM;
  173. goto out;
  174. }
  175. size *= sizeof(uint32_t);
  176. if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
  177. r = -EFAULT;
  178. goto out;
  179. }
  180. switch (p->chunks[i].chunk_id) {
  181. case AMDGPU_CHUNK_ID_IB:
  182. p->num_ibs++;
  183. break;
  184. case AMDGPU_CHUNK_ID_FENCE:
  185. size = sizeof(struct drm_amdgpu_cs_chunk_fence);
  186. if (p->chunks[i].length_dw * sizeof(uint32_t) >= size) {
  187. uint32_t handle;
  188. struct drm_gem_object *gobj;
  189. struct drm_amdgpu_cs_chunk_fence *fence_data;
  190. fence_data = (void *)p->chunks[i].kdata;
  191. handle = fence_data->handle;
  192. gobj = drm_gem_object_lookup(p->adev->ddev,
  193. p->filp, handle);
  194. if (gobj == NULL) {
  195. r = -EINVAL;
  196. goto out;
  197. }
  198. p->uf.bo = gem_to_amdgpu_bo(gobj);
  199. p->uf.offset = fence_data->offset;
  200. } else {
  201. r = -EINVAL;
  202. goto out;
  203. }
  204. break;
  205. case AMDGPU_CHUNK_ID_DEPENDENCIES:
  206. break;
  207. default:
  208. r = -EINVAL;
  209. goto out;
  210. }
  211. }
  212. p->ibs = kcalloc(p->num_ibs, sizeof(struct amdgpu_ib), GFP_KERNEL);
  213. if (!p->ibs) {
  214. r = -ENOMEM;
  215. goto out;
  216. }
  217. out:
  218. kfree(chunk_array);
  219. return r;
  220. }
  221. /* Returns how many bytes TTM can move per IB.
  222. */
  223. static u64 amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev)
  224. {
  225. u64 real_vram_size = adev->mc.real_vram_size;
  226. u64 vram_usage = atomic64_read(&adev->vram_usage);
  227. /* This function is based on the current VRAM usage.
  228. *
  229. * - If all of VRAM is free, allow relocating the number of bytes that
  230. * is equal to 1/4 of the size of VRAM for this IB.
  231. * - If more than one half of VRAM is occupied, only allow relocating
  232. * 1 MB of data for this IB.
  233. *
  234. * - From 0 to one half of used VRAM, the threshold decreases
  235. * linearly.
  236. * __________________
  237. * 1/4 of -|\ |
  238. * VRAM | \ |
  239. * | \ |
  240. * | \ |
  241. * | \ |
  242. * | \ |
  243. * | \ |
  244. * | \________|1 MB
  245. * |----------------|
  246. * VRAM 0 % 100 %
  247. * used used
  248. *
  249. * Note: It's a threshold, not a limit. The threshold must be crossed
  250. * for buffer relocations to stop, so any buffer of an arbitrary size
  251. * can be moved as long as the threshold isn't crossed before
  252. * the relocation takes place. We don't want to disable buffer
  253. * relocations completely.
  254. *
  255. * The idea is that buffers should be placed in VRAM at creation time
  256. * and TTM should only do a minimum number of relocations during
  257. * command submission. In practice, you need to submit at least
  258. * a dozen IBs to move all buffers to VRAM if they are in GTT.
  259. *
  260. * Also, things can get pretty crazy under memory pressure and actual
  261. * VRAM usage can change a lot, so playing safe even at 50% does
  262. * consistently increase performance.
  263. */
  264. u64 half_vram = real_vram_size >> 1;
  265. u64 half_free_vram = vram_usage >= half_vram ? 0 : half_vram - vram_usage;
  266. u64 bytes_moved_threshold = half_free_vram >> 1;
  267. return max(bytes_moved_threshold, 1024*1024ull);
  268. }
  269. int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p)
  270. {
  271. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  272. struct amdgpu_vm *vm = &fpriv->vm;
  273. struct amdgpu_device *adev = p->adev;
  274. struct amdgpu_bo_list_entry *lobj;
  275. struct list_head duplicates;
  276. struct amdgpu_bo *bo;
  277. u64 bytes_moved = 0, initial_bytes_moved;
  278. u64 bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(adev);
  279. int r;
  280. INIT_LIST_HEAD(&duplicates);
  281. r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true, &duplicates);
  282. if (unlikely(r != 0)) {
  283. return r;
  284. }
  285. list_for_each_entry(lobj, &p->validated, tv.head) {
  286. bo = lobj->robj;
  287. if (!bo->pin_count) {
  288. u32 domain = lobj->prefered_domains;
  289. u32 current_domain =
  290. amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
  291. /* Check if this buffer will be moved and don't move it
  292. * if we have moved too many buffers for this IB already.
  293. *
  294. * Note that this allows moving at least one buffer of
  295. * any size, because it doesn't take the current "bo"
  296. * into account. We don't want to disallow buffer moves
  297. * completely.
  298. */
  299. if (current_domain != AMDGPU_GEM_DOMAIN_CPU &&
  300. (domain & current_domain) == 0 && /* will be moved */
  301. bytes_moved > bytes_moved_threshold) {
  302. /* don't move it */
  303. domain = current_domain;
  304. }
  305. retry:
  306. amdgpu_ttm_placement_from_domain(bo, domain);
  307. initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
  308. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  309. bytes_moved += atomic64_read(&adev->num_bytes_moved) -
  310. initial_bytes_moved;
  311. if (unlikely(r)) {
  312. if (r != -ERESTARTSYS && domain != lobj->allowed_domains) {
  313. domain = lobj->allowed_domains;
  314. goto retry;
  315. }
  316. ttm_eu_backoff_reservation(&p->ticket, &p->validated);
  317. return r;
  318. }
  319. }
  320. lobj->bo_va = amdgpu_vm_bo_find(vm, bo);
  321. }
  322. return 0;
  323. }
  324. static int amdgpu_cs_parser_relocs(struct amdgpu_cs_parser *p)
  325. {
  326. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  327. struct amdgpu_cs_buckets buckets;
  328. bool need_mmap_lock = false;
  329. int i, r;
  330. if (p->bo_list) {
  331. need_mmap_lock = p->bo_list->has_userptr;
  332. amdgpu_cs_buckets_init(&buckets);
  333. for (i = 0; i < p->bo_list->num_entries; i++)
  334. amdgpu_cs_buckets_add(&buckets, &p->bo_list->array[i].tv.head,
  335. p->bo_list->array[i].priority);
  336. amdgpu_cs_buckets_get_list(&buckets, &p->validated);
  337. }
  338. p->vm_bos = amdgpu_vm_get_bos(p->adev, &fpriv->vm,
  339. &p->validated);
  340. if (need_mmap_lock)
  341. down_read(&current->mm->mmap_sem);
  342. r = amdgpu_cs_list_validate(p);
  343. if (need_mmap_lock)
  344. up_read(&current->mm->mmap_sem);
  345. return r;
  346. }
  347. static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
  348. {
  349. struct amdgpu_bo_list_entry *e;
  350. int r;
  351. list_for_each_entry(e, &p->validated, tv.head) {
  352. struct reservation_object *resv = e->robj->tbo.resv;
  353. r = amdgpu_sync_resv(p->adev, &p->ibs[0].sync, resv, p->filp);
  354. if (r)
  355. return r;
  356. }
  357. return 0;
  358. }
  359. static int cmp_size_smaller_first(void *priv, struct list_head *a,
  360. struct list_head *b)
  361. {
  362. struct amdgpu_bo_list_entry *la = list_entry(a, struct amdgpu_bo_list_entry, tv.head);
  363. struct amdgpu_bo_list_entry *lb = list_entry(b, struct amdgpu_bo_list_entry, tv.head);
  364. /* Sort A before B if A is smaller. */
  365. return (int)la->robj->tbo.num_pages - (int)lb->robj->tbo.num_pages;
  366. }
  367. /**
  368. * cs_parser_fini() - clean parser states
  369. * @parser: parser structure holding parsing context.
  370. * @error: error number
  371. *
  372. * If error is set than unvalidate buffer, otherwise just free memory
  373. * used by parsing context.
  374. **/
  375. static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
  376. {
  377. unsigned i;
  378. if (!error) {
  379. /* Sort the buffer list from the smallest to largest buffer,
  380. * which affects the order of buffers in the LRU list.
  381. * This assures that the smallest buffers are added first
  382. * to the LRU list, so they are likely to be later evicted
  383. * first, instead of large buffers whose eviction is more
  384. * expensive.
  385. *
  386. * This slightly lowers the number of bytes moved by TTM
  387. * per frame under memory pressure.
  388. */
  389. list_sort(NULL, &parser->validated, cmp_size_smaller_first);
  390. ttm_eu_fence_buffer_objects(&parser->ticket,
  391. &parser->validated,
  392. &parser->ibs[parser->num_ibs-1].fence->base);
  393. } else if (backoff) {
  394. ttm_eu_backoff_reservation(&parser->ticket,
  395. &parser->validated);
  396. }
  397. if (parser->ctx)
  398. amdgpu_ctx_put(parser->ctx);
  399. if (parser->bo_list)
  400. amdgpu_bo_list_put(parser->bo_list);
  401. drm_free_large(parser->vm_bos);
  402. for (i = 0; i < parser->nchunks; i++)
  403. drm_free_large(parser->chunks[i].kdata);
  404. kfree(parser->chunks);
  405. if (parser->ibs)
  406. for (i = 0; i < parser->num_ibs; i++)
  407. amdgpu_ib_free(parser->adev, &parser->ibs[i]);
  408. kfree(parser->ibs);
  409. if (parser->uf.bo)
  410. drm_gem_object_unreference_unlocked(&parser->uf.bo->gem_base);
  411. }
  412. static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p,
  413. struct amdgpu_vm *vm)
  414. {
  415. struct amdgpu_device *adev = p->adev;
  416. struct amdgpu_bo_va *bo_va;
  417. struct amdgpu_bo *bo;
  418. int i, r;
  419. r = amdgpu_vm_update_page_directory(adev, vm);
  420. if (r)
  421. return r;
  422. r = amdgpu_vm_clear_freed(adev, vm);
  423. if (r)
  424. return r;
  425. if (p->bo_list) {
  426. for (i = 0; i < p->bo_list->num_entries; i++) {
  427. struct fence *f;
  428. /* ignore duplicates */
  429. bo = p->bo_list->array[i].robj;
  430. if (!bo)
  431. continue;
  432. bo_va = p->bo_list->array[i].bo_va;
  433. if (bo_va == NULL)
  434. continue;
  435. r = amdgpu_vm_bo_update(adev, bo_va, &bo->tbo.mem);
  436. if (r)
  437. return r;
  438. f = &bo_va->last_pt_update->base;
  439. r = amdgpu_sync_fence(adev, &p->ibs[0].sync, f);
  440. if (r)
  441. return r;
  442. }
  443. }
  444. return amdgpu_vm_clear_invalids(adev, vm, &p->ibs[0].sync);
  445. }
  446. static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
  447. struct amdgpu_cs_parser *parser)
  448. {
  449. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  450. struct amdgpu_vm *vm = &fpriv->vm;
  451. struct amdgpu_ring *ring;
  452. int i, r;
  453. if (parser->num_ibs == 0)
  454. return 0;
  455. /* Only for UVD/VCE VM emulation */
  456. for (i = 0; i < parser->num_ibs; i++) {
  457. ring = parser->ibs[i].ring;
  458. if (ring->funcs->parse_cs) {
  459. r = amdgpu_ring_parse_cs(ring, parser, i);
  460. if (r)
  461. return r;
  462. }
  463. }
  464. mutex_lock(&vm->mutex);
  465. r = amdgpu_bo_vm_update_pte(parser, vm);
  466. if (r) {
  467. goto out;
  468. }
  469. amdgpu_cs_sync_rings(parser);
  470. r = amdgpu_ib_schedule(adev, parser->num_ibs, parser->ibs,
  471. parser->filp);
  472. out:
  473. mutex_unlock(&vm->mutex);
  474. return r;
  475. }
  476. static int amdgpu_cs_handle_lockup(struct amdgpu_device *adev, int r)
  477. {
  478. if (r == -EDEADLK) {
  479. r = amdgpu_gpu_reset(adev);
  480. if (!r)
  481. r = -EAGAIN;
  482. }
  483. return r;
  484. }
  485. static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
  486. struct amdgpu_cs_parser *parser)
  487. {
  488. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  489. struct amdgpu_vm *vm = &fpriv->vm;
  490. int i, j;
  491. int r;
  492. for (i = 0, j = 0; i < parser->nchunks && j < parser->num_ibs; i++) {
  493. struct amdgpu_cs_chunk *chunk;
  494. struct amdgpu_ib *ib;
  495. struct drm_amdgpu_cs_chunk_ib *chunk_ib;
  496. struct amdgpu_ring *ring;
  497. chunk = &parser->chunks[i];
  498. ib = &parser->ibs[j];
  499. chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
  500. if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
  501. continue;
  502. r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
  503. chunk_ib->ip_instance, chunk_ib->ring,
  504. &ring);
  505. if (r)
  506. return r;
  507. if (ring->funcs->parse_cs) {
  508. struct amdgpu_bo_va_mapping *m;
  509. struct amdgpu_bo *aobj = NULL;
  510. uint64_t offset;
  511. uint8_t *kptr;
  512. m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
  513. &aobj);
  514. if (!aobj) {
  515. DRM_ERROR("IB va_start is invalid\n");
  516. return -EINVAL;
  517. }
  518. if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
  519. (m->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
  520. DRM_ERROR("IB va_start+ib_bytes is invalid\n");
  521. return -EINVAL;
  522. }
  523. /* the IB should be reserved at this point */
  524. r = amdgpu_bo_kmap(aobj, (void **)&kptr);
  525. if (r) {
  526. return r;
  527. }
  528. offset = ((uint64_t)m->it.start) * AMDGPU_GPU_PAGE_SIZE;
  529. kptr += chunk_ib->va_start - offset;
  530. r = amdgpu_ib_get(ring, NULL, chunk_ib->ib_bytes, ib);
  531. if (r) {
  532. DRM_ERROR("Failed to get ib !\n");
  533. return r;
  534. }
  535. memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
  536. amdgpu_bo_kunmap(aobj);
  537. } else {
  538. r = amdgpu_ib_get(ring, vm, 0, ib);
  539. if (r) {
  540. DRM_ERROR("Failed to get ib !\n");
  541. return r;
  542. }
  543. ib->gpu_addr = chunk_ib->va_start;
  544. }
  545. ib->length_dw = chunk_ib->ib_bytes / 4;
  546. ib->flags = chunk_ib->flags;
  547. ib->ctx = parser->ctx;
  548. j++;
  549. }
  550. if (!parser->num_ibs)
  551. return 0;
  552. /* add GDS resources to first IB */
  553. if (parser->bo_list) {
  554. struct amdgpu_bo *gds = parser->bo_list->gds_obj;
  555. struct amdgpu_bo *gws = parser->bo_list->gws_obj;
  556. struct amdgpu_bo *oa = parser->bo_list->oa_obj;
  557. struct amdgpu_ib *ib = &parser->ibs[0];
  558. if (gds) {
  559. ib->gds_base = amdgpu_bo_gpu_offset(gds);
  560. ib->gds_size = amdgpu_bo_size(gds);
  561. }
  562. if (gws) {
  563. ib->gws_base = amdgpu_bo_gpu_offset(gws);
  564. ib->gws_size = amdgpu_bo_size(gws);
  565. }
  566. if (oa) {
  567. ib->oa_base = amdgpu_bo_gpu_offset(oa);
  568. ib->oa_size = amdgpu_bo_size(oa);
  569. }
  570. }
  571. /* wrap the last IB with user fence */
  572. if (parser->uf.bo) {
  573. struct amdgpu_ib *ib = &parser->ibs[parser->num_ibs - 1];
  574. /* UVD & VCE fw doesn't support user fences */
  575. if (ib->ring->type == AMDGPU_RING_TYPE_UVD ||
  576. ib->ring->type == AMDGPU_RING_TYPE_VCE)
  577. return -EINVAL;
  578. ib->user = &parser->uf;
  579. }
  580. return 0;
  581. }
  582. static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
  583. struct amdgpu_cs_parser *p)
  584. {
  585. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  586. struct amdgpu_ib *ib;
  587. int i, j, r;
  588. if (!p->num_ibs)
  589. return 0;
  590. /* Add dependencies to first IB */
  591. ib = &p->ibs[0];
  592. for (i = 0; i < p->nchunks; ++i) {
  593. struct drm_amdgpu_cs_chunk_dep *deps;
  594. struct amdgpu_cs_chunk *chunk;
  595. unsigned num_deps;
  596. chunk = &p->chunks[i];
  597. if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES)
  598. continue;
  599. deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
  600. num_deps = chunk->length_dw * 4 /
  601. sizeof(struct drm_amdgpu_cs_chunk_dep);
  602. for (j = 0; j < num_deps; ++j) {
  603. struct amdgpu_ring *ring;
  604. struct amdgpu_ctx *ctx;
  605. struct fence *fence;
  606. r = amdgpu_cs_get_ring(adev, deps[j].ip_type,
  607. deps[j].ip_instance,
  608. deps[j].ring, &ring);
  609. if (r)
  610. return r;
  611. ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id);
  612. if (ctx == NULL)
  613. return -EINVAL;
  614. fence = amdgpu_ctx_get_fence(ctx, ring,
  615. deps[j].handle);
  616. if (IS_ERR(fence)) {
  617. r = PTR_ERR(fence);
  618. amdgpu_ctx_put(ctx);
  619. return r;
  620. } else if (fence) {
  621. r = amdgpu_sync_fence(adev, &ib->sync, fence);
  622. fence_put(fence);
  623. amdgpu_ctx_put(ctx);
  624. if (r)
  625. return r;
  626. }
  627. }
  628. }
  629. return 0;
  630. }
  631. int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  632. {
  633. struct amdgpu_device *adev = dev->dev_private;
  634. union drm_amdgpu_cs *cs = data;
  635. struct amdgpu_cs_parser parser;
  636. int r, i;
  637. bool reserved_buffers = false;
  638. down_read(&adev->exclusive_lock);
  639. if (!adev->accel_working) {
  640. up_read(&adev->exclusive_lock);
  641. return -EBUSY;
  642. }
  643. /* initialize parser */
  644. memset(&parser, 0, sizeof(struct amdgpu_cs_parser));
  645. parser.filp = filp;
  646. parser.adev = adev;
  647. r = amdgpu_cs_parser_init(&parser, data);
  648. if (r) {
  649. DRM_ERROR("Failed to initialize parser !\n");
  650. amdgpu_cs_parser_fini(&parser, r, false);
  651. up_read(&adev->exclusive_lock);
  652. r = amdgpu_cs_handle_lockup(adev, r);
  653. return r;
  654. }
  655. r = amdgpu_cs_parser_relocs(&parser);
  656. if (r) {
  657. if (r != -ERESTARTSYS) {
  658. if (r == -ENOMEM)
  659. DRM_ERROR("Not enough memory for command submission!\n");
  660. else
  661. DRM_ERROR("Failed to process the buffer list %d!\n", r);
  662. }
  663. }
  664. if (!r) {
  665. reserved_buffers = true;
  666. r = amdgpu_cs_ib_fill(adev, &parser);
  667. }
  668. if (!r) {
  669. r = amdgpu_cs_dependencies(adev, &parser);
  670. if (r)
  671. DRM_ERROR("Failed in the dependencies handling %d!\n", r);
  672. }
  673. if (r) {
  674. amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
  675. up_read(&adev->exclusive_lock);
  676. r = amdgpu_cs_handle_lockup(adev, r);
  677. return r;
  678. }
  679. for (i = 0; i < parser.num_ibs; i++)
  680. trace_amdgpu_cs(&parser, i);
  681. r = amdgpu_cs_ib_vm_chunk(adev, &parser);
  682. if (r) {
  683. goto out;
  684. }
  685. cs->out.handle = parser.uf.sequence;
  686. out:
  687. amdgpu_cs_parser_fini(&parser, r, true);
  688. up_read(&adev->exclusive_lock);
  689. r = amdgpu_cs_handle_lockup(adev, r);
  690. return r;
  691. }
  692. /**
  693. * amdgpu_cs_wait_ioctl - wait for a command submission to finish
  694. *
  695. * @dev: drm device
  696. * @data: data from userspace
  697. * @filp: file private
  698. *
  699. * Wait for the command submission identified by handle to finish.
  700. */
  701. int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
  702. struct drm_file *filp)
  703. {
  704. union drm_amdgpu_wait_cs *wait = data;
  705. struct amdgpu_device *adev = dev->dev_private;
  706. unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
  707. struct amdgpu_ring *ring = NULL;
  708. struct amdgpu_ctx *ctx;
  709. struct fence *fence;
  710. long r;
  711. r = amdgpu_cs_get_ring(adev, wait->in.ip_type, wait->in.ip_instance,
  712. wait->in.ring, &ring);
  713. if (r)
  714. return r;
  715. ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
  716. if (ctx == NULL)
  717. return -EINVAL;
  718. fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
  719. if (IS_ERR(fence))
  720. r = PTR_ERR(fence);
  721. else if (fence) {
  722. r = fence_wait_timeout(fence, true, timeout);
  723. fence_put(fence);
  724. } else
  725. r = 1;
  726. amdgpu_ctx_put(ctx);
  727. if (r < 0)
  728. return r;
  729. memset(wait, 0, sizeof(*wait));
  730. wait->out.status = (r == 0);
  731. return 0;
  732. }
  733. /**
  734. * amdgpu_cs_find_bo_va - find bo_va for VM address
  735. *
  736. * @parser: command submission parser context
  737. * @addr: VM address
  738. * @bo: resulting BO of the mapping found
  739. *
  740. * Search the buffer objects in the command submission context for a certain
  741. * virtual memory address. Returns allocation structure when found, NULL
  742. * otherwise.
  743. */
  744. struct amdgpu_bo_va_mapping *
  745. amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
  746. uint64_t addr, struct amdgpu_bo **bo)
  747. {
  748. struct amdgpu_bo_list_entry *reloc;
  749. struct amdgpu_bo_va_mapping *mapping;
  750. addr /= AMDGPU_GPU_PAGE_SIZE;
  751. list_for_each_entry(reloc, &parser->validated, tv.head) {
  752. if (!reloc->bo_va)
  753. continue;
  754. list_for_each_entry(mapping, &reloc->bo_va->mappings, list) {
  755. if (mapping->it.start > addr ||
  756. addr > mapping->it.last)
  757. continue;
  758. *bo = reloc->bo_va->bo;
  759. return mapping;
  760. }
  761. }
  762. return NULL;
  763. }