amdgpu_cs.c 22 KB

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