amdgpu_cs.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  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. static int amdgpu_cs_user_fence_chunk(struct amdgpu_cs_parser *p,
  33. struct drm_amdgpu_cs_chunk_fence *data,
  34. uint32_t *offset)
  35. {
  36. struct drm_gem_object *gobj;
  37. unsigned long size;
  38. gobj = drm_gem_object_lookup(p->filp, data->handle);
  39. if (gobj == NULL)
  40. return -EINVAL;
  41. p->uf_entry.robj = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
  42. p->uf_entry.priority = 0;
  43. p->uf_entry.tv.bo = &p->uf_entry.robj->tbo;
  44. p->uf_entry.tv.shared = true;
  45. p->uf_entry.user_pages = NULL;
  46. size = amdgpu_bo_size(p->uf_entry.robj);
  47. if (size != PAGE_SIZE || (data->offset + 8) > size)
  48. return -EINVAL;
  49. *offset = data->offset;
  50. drm_gem_object_unreference_unlocked(gobj);
  51. if (amdgpu_ttm_tt_get_usermm(p->uf_entry.robj->tbo.ttm)) {
  52. amdgpu_bo_unref(&p->uf_entry.robj);
  53. return -EINVAL;
  54. }
  55. return 0;
  56. }
  57. int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, void *data)
  58. {
  59. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  60. struct amdgpu_vm *vm = &fpriv->vm;
  61. union drm_amdgpu_cs *cs = data;
  62. uint64_t *chunk_array_user;
  63. uint64_t *chunk_array;
  64. unsigned size, num_ibs = 0;
  65. uint32_t uf_offset = 0;
  66. int i;
  67. int ret;
  68. if (cs->in.num_chunks == 0)
  69. return 0;
  70. chunk_array = kmalloc_array(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL);
  71. if (!chunk_array)
  72. return -ENOMEM;
  73. p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id);
  74. if (!p->ctx) {
  75. ret = -EINVAL;
  76. goto free_chunk;
  77. }
  78. /* get chunks */
  79. chunk_array_user = (uint64_t __user *)(uintptr_t)(cs->in.chunks);
  80. if (copy_from_user(chunk_array, chunk_array_user,
  81. sizeof(uint64_t)*cs->in.num_chunks)) {
  82. ret = -EFAULT;
  83. goto put_ctx;
  84. }
  85. p->nchunks = cs->in.num_chunks;
  86. p->chunks = kmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk),
  87. GFP_KERNEL);
  88. if (!p->chunks) {
  89. ret = -ENOMEM;
  90. goto put_ctx;
  91. }
  92. for (i = 0; i < p->nchunks; i++) {
  93. struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL;
  94. struct drm_amdgpu_cs_chunk user_chunk;
  95. uint32_t __user *cdata;
  96. chunk_ptr = (void __user *)(uintptr_t)chunk_array[i];
  97. if (copy_from_user(&user_chunk, chunk_ptr,
  98. sizeof(struct drm_amdgpu_cs_chunk))) {
  99. ret = -EFAULT;
  100. i--;
  101. goto free_partial_kdata;
  102. }
  103. p->chunks[i].chunk_id = user_chunk.chunk_id;
  104. p->chunks[i].length_dw = user_chunk.length_dw;
  105. size = p->chunks[i].length_dw;
  106. cdata = (void __user *)(uintptr_t)user_chunk.chunk_data;
  107. p->chunks[i].kdata = kvmalloc_array(size, sizeof(uint32_t), GFP_KERNEL);
  108. if (p->chunks[i].kdata == NULL) {
  109. ret = -ENOMEM;
  110. i--;
  111. goto free_partial_kdata;
  112. }
  113. size *= sizeof(uint32_t);
  114. if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
  115. ret = -EFAULT;
  116. goto free_partial_kdata;
  117. }
  118. switch (p->chunks[i].chunk_id) {
  119. case AMDGPU_CHUNK_ID_IB:
  120. ++num_ibs;
  121. break;
  122. case AMDGPU_CHUNK_ID_FENCE:
  123. size = sizeof(struct drm_amdgpu_cs_chunk_fence);
  124. if (p->chunks[i].length_dw * sizeof(uint32_t) < size) {
  125. ret = -EINVAL;
  126. goto free_partial_kdata;
  127. }
  128. ret = amdgpu_cs_user_fence_chunk(p, p->chunks[i].kdata,
  129. &uf_offset);
  130. if (ret)
  131. goto free_partial_kdata;
  132. break;
  133. case AMDGPU_CHUNK_ID_DEPENDENCIES:
  134. break;
  135. default:
  136. ret = -EINVAL;
  137. goto free_partial_kdata;
  138. }
  139. }
  140. ret = amdgpu_job_alloc(p->adev, num_ibs, &p->job, vm);
  141. if (ret)
  142. goto free_all_kdata;
  143. if (p->uf_entry.robj)
  144. p->job->uf_addr = uf_offset;
  145. kfree(chunk_array);
  146. return 0;
  147. free_all_kdata:
  148. i = p->nchunks - 1;
  149. free_partial_kdata:
  150. for (; i >= 0; i--)
  151. kvfree(p->chunks[i].kdata);
  152. kfree(p->chunks);
  153. p->chunks = NULL;
  154. p->nchunks = 0;
  155. put_ctx:
  156. amdgpu_ctx_put(p->ctx);
  157. free_chunk:
  158. kfree(chunk_array);
  159. return ret;
  160. }
  161. /* Convert microseconds to bytes. */
  162. static u64 us_to_bytes(struct amdgpu_device *adev, s64 us)
  163. {
  164. if (us <= 0 || !adev->mm_stats.log2_max_MBps)
  165. return 0;
  166. /* Since accum_us is incremented by a million per second, just
  167. * multiply it by the number of MB/s to get the number of bytes.
  168. */
  169. return us << adev->mm_stats.log2_max_MBps;
  170. }
  171. static s64 bytes_to_us(struct amdgpu_device *adev, u64 bytes)
  172. {
  173. if (!adev->mm_stats.log2_max_MBps)
  174. return 0;
  175. return bytes >> adev->mm_stats.log2_max_MBps;
  176. }
  177. /* Returns how many bytes TTM can move right now. If no bytes can be moved,
  178. * it returns 0. If it returns non-zero, it's OK to move at least one buffer,
  179. * which means it can go over the threshold once. If that happens, the driver
  180. * will be in debt and no other buffer migrations can be done until that debt
  181. * is repaid.
  182. *
  183. * This approach allows moving a buffer of any size (it's important to allow
  184. * that).
  185. *
  186. * The currency is simply time in microseconds and it increases as the clock
  187. * ticks. The accumulated microseconds (us) are converted to bytes and
  188. * returned.
  189. */
  190. static u64 amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev)
  191. {
  192. s64 time_us, increment_us;
  193. u64 max_bytes;
  194. u64 free_vram, total_vram, used_vram;
  195. /* Allow a maximum of 200 accumulated ms. This is basically per-IB
  196. * throttling.
  197. *
  198. * It means that in order to get full max MBps, at least 5 IBs per
  199. * second must be submitted and not more than 200ms apart from each
  200. * other.
  201. */
  202. const s64 us_upper_bound = 200000;
  203. if (!adev->mm_stats.log2_max_MBps)
  204. return 0;
  205. total_vram = adev->mc.real_vram_size - adev->vram_pin_size;
  206. used_vram = atomic64_read(&adev->vram_usage);
  207. free_vram = used_vram >= total_vram ? 0 : total_vram - used_vram;
  208. spin_lock(&adev->mm_stats.lock);
  209. /* Increase the amount of accumulated us. */
  210. time_us = ktime_to_us(ktime_get());
  211. increment_us = time_us - adev->mm_stats.last_update_us;
  212. adev->mm_stats.last_update_us = time_us;
  213. adev->mm_stats.accum_us = min(adev->mm_stats.accum_us + increment_us,
  214. us_upper_bound);
  215. /* This prevents the short period of low performance when the VRAM
  216. * usage is low and the driver is in debt or doesn't have enough
  217. * accumulated us to fill VRAM quickly.
  218. *
  219. * The situation can occur in these cases:
  220. * - a lot of VRAM is freed by userspace
  221. * - the presence of a big buffer causes a lot of evictions
  222. * (solution: split buffers into smaller ones)
  223. *
  224. * If 128 MB or 1/8th of VRAM is free, start filling it now by setting
  225. * accum_us to a positive number.
  226. */
  227. if (free_vram >= 128 * 1024 * 1024 || free_vram >= total_vram / 8) {
  228. s64 min_us;
  229. /* Be more aggresive on dGPUs. Try to fill a portion of free
  230. * VRAM now.
  231. */
  232. if (!(adev->flags & AMD_IS_APU))
  233. min_us = bytes_to_us(adev, free_vram / 4);
  234. else
  235. min_us = 0; /* Reset accum_us on APUs. */
  236. adev->mm_stats.accum_us = max(min_us, adev->mm_stats.accum_us);
  237. }
  238. /* This returns 0 if the driver is in debt to disallow (optional)
  239. * buffer moves.
  240. */
  241. max_bytes = us_to_bytes(adev, adev->mm_stats.accum_us);
  242. spin_unlock(&adev->mm_stats.lock);
  243. return max_bytes;
  244. }
  245. /* Report how many bytes have really been moved for the last command
  246. * submission. This can result in a debt that can stop buffer migrations
  247. * temporarily.
  248. */
  249. void amdgpu_cs_report_moved_bytes(struct amdgpu_device *adev, u64 num_bytes)
  250. {
  251. spin_lock(&adev->mm_stats.lock);
  252. adev->mm_stats.accum_us -= bytes_to_us(adev, num_bytes);
  253. spin_unlock(&adev->mm_stats.lock);
  254. }
  255. static int amdgpu_cs_bo_validate(struct amdgpu_cs_parser *p,
  256. struct amdgpu_bo *bo)
  257. {
  258. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  259. u64 initial_bytes_moved;
  260. uint32_t domain;
  261. int r;
  262. if (bo->pin_count)
  263. return 0;
  264. /* Don't move this buffer if we have depleted our allowance
  265. * to move it. Don't move anything if the threshold is zero.
  266. */
  267. if (p->bytes_moved < p->bytes_moved_threshold)
  268. domain = bo->prefered_domains;
  269. else
  270. domain = bo->allowed_domains;
  271. retry:
  272. amdgpu_ttm_placement_from_domain(bo, domain);
  273. initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
  274. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  275. p->bytes_moved += atomic64_read(&adev->num_bytes_moved) -
  276. initial_bytes_moved;
  277. if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) {
  278. domain = bo->allowed_domains;
  279. goto retry;
  280. }
  281. return r;
  282. }
  283. /* Last resort, try to evict something from the current working set */
  284. static bool amdgpu_cs_try_evict(struct amdgpu_cs_parser *p,
  285. struct amdgpu_bo *validated)
  286. {
  287. uint32_t domain = validated->allowed_domains;
  288. int r;
  289. if (!p->evictable)
  290. return false;
  291. for (;&p->evictable->tv.head != &p->validated;
  292. p->evictable = list_prev_entry(p->evictable, tv.head)) {
  293. struct amdgpu_bo_list_entry *candidate = p->evictable;
  294. struct amdgpu_bo *bo = candidate->robj;
  295. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  296. u64 initial_bytes_moved;
  297. uint32_t other;
  298. /* If we reached our current BO we can forget it */
  299. if (candidate->robj == validated)
  300. break;
  301. other = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
  302. /* Check if this BO is in one of the domains we need space for */
  303. if (!(other & domain))
  304. continue;
  305. /* Check if we can move this BO somewhere else */
  306. other = bo->allowed_domains & ~domain;
  307. if (!other)
  308. continue;
  309. /* Good we can try to move this BO somewhere else */
  310. amdgpu_ttm_placement_from_domain(bo, other);
  311. initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
  312. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  313. p->bytes_moved += atomic64_read(&adev->num_bytes_moved) -
  314. initial_bytes_moved;
  315. if (unlikely(r))
  316. break;
  317. p->evictable = list_prev_entry(p->evictable, tv.head);
  318. list_move(&candidate->tv.head, &p->validated);
  319. return true;
  320. }
  321. return false;
  322. }
  323. static int amdgpu_cs_validate(void *param, struct amdgpu_bo *bo)
  324. {
  325. struct amdgpu_cs_parser *p = param;
  326. int r;
  327. do {
  328. r = amdgpu_cs_bo_validate(p, bo);
  329. } while (r == -ENOMEM && amdgpu_cs_try_evict(p, bo));
  330. if (r)
  331. return r;
  332. if (bo->shadow)
  333. r = amdgpu_cs_bo_validate(p, bo->shadow);
  334. return r;
  335. }
  336. static int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p,
  337. struct list_head *validated)
  338. {
  339. struct amdgpu_bo_list_entry *lobj;
  340. int r;
  341. list_for_each_entry(lobj, validated, tv.head) {
  342. struct amdgpu_bo *bo = lobj->robj;
  343. bool binding_userptr = false;
  344. struct mm_struct *usermm;
  345. usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
  346. if (usermm && usermm != current->mm)
  347. return -EPERM;
  348. /* Check if we have user pages and nobody bound the BO already */
  349. if (lobj->user_pages && bo->tbo.ttm->state != tt_bound) {
  350. size_t size = sizeof(struct page *);
  351. size *= bo->tbo.ttm->num_pages;
  352. memcpy(bo->tbo.ttm->pages, lobj->user_pages, size);
  353. binding_userptr = true;
  354. }
  355. if (p->evictable == lobj)
  356. p->evictable = NULL;
  357. r = amdgpu_cs_validate(p, bo);
  358. if (r)
  359. return r;
  360. if (binding_userptr) {
  361. kvfree(lobj->user_pages);
  362. lobj->user_pages = NULL;
  363. }
  364. }
  365. return 0;
  366. }
  367. static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
  368. union drm_amdgpu_cs *cs)
  369. {
  370. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  371. struct amdgpu_bo_list_entry *e;
  372. struct list_head duplicates;
  373. bool need_mmap_lock = false;
  374. unsigned i, tries = 10;
  375. int r;
  376. INIT_LIST_HEAD(&p->validated);
  377. p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
  378. if (p->bo_list) {
  379. need_mmap_lock = p->bo_list->first_userptr !=
  380. p->bo_list->num_entries;
  381. amdgpu_bo_list_get_list(p->bo_list, &p->validated);
  382. }
  383. INIT_LIST_HEAD(&duplicates);
  384. amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
  385. if (p->uf_entry.robj)
  386. list_add(&p->uf_entry.tv.head, &p->validated);
  387. if (need_mmap_lock)
  388. down_read(&current->mm->mmap_sem);
  389. while (1) {
  390. struct list_head need_pages;
  391. unsigned i;
  392. r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
  393. &duplicates);
  394. if (unlikely(r != 0)) {
  395. if (r != -ERESTARTSYS)
  396. DRM_ERROR("ttm_eu_reserve_buffers failed.\n");
  397. goto error_free_pages;
  398. }
  399. /* Without a BO list we don't have userptr BOs */
  400. if (!p->bo_list)
  401. break;
  402. INIT_LIST_HEAD(&need_pages);
  403. for (i = p->bo_list->first_userptr;
  404. i < p->bo_list->num_entries; ++i) {
  405. e = &p->bo_list->array[i];
  406. if (amdgpu_ttm_tt_userptr_invalidated(e->robj->tbo.ttm,
  407. &e->user_invalidated) && e->user_pages) {
  408. /* We acquired a page array, but somebody
  409. * invalidated it. Free it an try again
  410. */
  411. release_pages(e->user_pages,
  412. e->robj->tbo.ttm->num_pages,
  413. false);
  414. kvfree(e->user_pages);
  415. e->user_pages = NULL;
  416. }
  417. if (e->robj->tbo.ttm->state != tt_bound &&
  418. !e->user_pages) {
  419. list_del(&e->tv.head);
  420. list_add(&e->tv.head, &need_pages);
  421. amdgpu_bo_unreserve(e->robj);
  422. }
  423. }
  424. if (list_empty(&need_pages))
  425. break;
  426. /* Unreserve everything again. */
  427. ttm_eu_backoff_reservation(&p->ticket, &p->validated);
  428. /* We tried too many times, just abort */
  429. if (!--tries) {
  430. r = -EDEADLK;
  431. DRM_ERROR("deadlock in %s\n", __func__);
  432. goto error_free_pages;
  433. }
  434. /* Fill the page arrays for all userptrs. */
  435. list_for_each_entry(e, &need_pages, tv.head) {
  436. struct ttm_tt *ttm = e->robj->tbo.ttm;
  437. e->user_pages = kvmalloc_array(ttm->num_pages,
  438. sizeof(struct page*),
  439. GFP_KERNEL | __GFP_ZERO);
  440. if (!e->user_pages) {
  441. r = -ENOMEM;
  442. DRM_ERROR("calloc failure in %s\n", __func__);
  443. goto error_free_pages;
  444. }
  445. r = amdgpu_ttm_tt_get_user_pages(ttm, e->user_pages);
  446. if (r) {
  447. DRM_ERROR("amdgpu_ttm_tt_get_user_pages failed.\n");
  448. kvfree(e->user_pages);
  449. e->user_pages = NULL;
  450. goto error_free_pages;
  451. }
  452. }
  453. /* And try again. */
  454. list_splice(&need_pages, &p->validated);
  455. }
  456. p->bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(p->adev);
  457. p->bytes_moved = 0;
  458. p->evictable = list_last_entry(&p->validated,
  459. struct amdgpu_bo_list_entry,
  460. tv.head);
  461. r = amdgpu_vm_validate_pt_bos(p->adev, &fpriv->vm,
  462. amdgpu_cs_validate, p);
  463. if (r) {
  464. DRM_ERROR("amdgpu_vm_validate_pt_bos() failed.\n");
  465. goto error_validate;
  466. }
  467. r = amdgpu_cs_list_validate(p, &duplicates);
  468. if (r) {
  469. DRM_ERROR("amdgpu_cs_list_validate(duplicates) failed.\n");
  470. goto error_validate;
  471. }
  472. r = amdgpu_cs_list_validate(p, &p->validated);
  473. if (r) {
  474. DRM_ERROR("amdgpu_cs_list_validate(validated) failed.\n");
  475. goto error_validate;
  476. }
  477. amdgpu_cs_report_moved_bytes(p->adev, p->bytes_moved);
  478. fpriv->vm.last_eviction_counter =
  479. atomic64_read(&p->adev->num_evictions);
  480. if (p->bo_list) {
  481. struct amdgpu_bo *gds = p->bo_list->gds_obj;
  482. struct amdgpu_bo *gws = p->bo_list->gws_obj;
  483. struct amdgpu_bo *oa = p->bo_list->oa_obj;
  484. struct amdgpu_vm *vm = &fpriv->vm;
  485. unsigned i;
  486. for (i = 0; i < p->bo_list->num_entries; i++) {
  487. struct amdgpu_bo *bo = p->bo_list->array[i].robj;
  488. p->bo_list->array[i].bo_va = amdgpu_vm_bo_find(vm, bo);
  489. }
  490. if (gds) {
  491. p->job->gds_base = amdgpu_bo_gpu_offset(gds);
  492. p->job->gds_size = amdgpu_bo_size(gds);
  493. }
  494. if (gws) {
  495. p->job->gws_base = amdgpu_bo_gpu_offset(gws);
  496. p->job->gws_size = amdgpu_bo_size(gws);
  497. }
  498. if (oa) {
  499. p->job->oa_base = amdgpu_bo_gpu_offset(oa);
  500. p->job->oa_size = amdgpu_bo_size(oa);
  501. }
  502. }
  503. if (!r && p->uf_entry.robj) {
  504. struct amdgpu_bo *uf = p->uf_entry.robj;
  505. r = amdgpu_ttm_bind(&uf->tbo, &uf->tbo.mem);
  506. p->job->uf_addr += amdgpu_bo_gpu_offset(uf);
  507. }
  508. error_validate:
  509. if (r) {
  510. amdgpu_vm_move_pt_bos_in_lru(p->adev, &fpriv->vm);
  511. ttm_eu_backoff_reservation(&p->ticket, &p->validated);
  512. }
  513. error_free_pages:
  514. if (need_mmap_lock)
  515. up_read(&current->mm->mmap_sem);
  516. if (p->bo_list) {
  517. for (i = p->bo_list->first_userptr;
  518. i < p->bo_list->num_entries; ++i) {
  519. e = &p->bo_list->array[i];
  520. if (!e->user_pages)
  521. continue;
  522. release_pages(e->user_pages,
  523. e->robj->tbo.ttm->num_pages,
  524. false);
  525. kvfree(e->user_pages);
  526. }
  527. }
  528. return r;
  529. }
  530. static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
  531. {
  532. struct amdgpu_bo_list_entry *e;
  533. int r;
  534. list_for_each_entry(e, &p->validated, tv.head) {
  535. struct reservation_object *resv = e->robj->tbo.resv;
  536. r = amdgpu_sync_resv(p->adev, &p->job->sync, resv, p->filp);
  537. if (r)
  538. return r;
  539. }
  540. return 0;
  541. }
  542. /**
  543. * cs_parser_fini() - clean parser states
  544. * @parser: parser structure holding parsing context.
  545. * @error: error number
  546. *
  547. * If error is set than unvalidate buffer, otherwise just free memory
  548. * used by parsing context.
  549. **/
  550. static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
  551. {
  552. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  553. unsigned i;
  554. if (!error) {
  555. amdgpu_vm_move_pt_bos_in_lru(parser->adev, &fpriv->vm);
  556. ttm_eu_fence_buffer_objects(&parser->ticket,
  557. &parser->validated,
  558. parser->fence);
  559. } else if (backoff) {
  560. ttm_eu_backoff_reservation(&parser->ticket,
  561. &parser->validated);
  562. }
  563. dma_fence_put(parser->fence);
  564. if (parser->ctx)
  565. amdgpu_ctx_put(parser->ctx);
  566. if (parser->bo_list)
  567. amdgpu_bo_list_put(parser->bo_list);
  568. for (i = 0; i < parser->nchunks; i++)
  569. kvfree(parser->chunks[i].kdata);
  570. kfree(parser->chunks);
  571. if (parser->job)
  572. amdgpu_job_free(parser->job);
  573. amdgpu_bo_unref(&parser->uf_entry.robj);
  574. }
  575. static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p)
  576. {
  577. struct amdgpu_device *adev = p->adev;
  578. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  579. struct amdgpu_vm *vm = &fpriv->vm;
  580. struct amdgpu_bo_va *bo_va;
  581. struct amdgpu_bo *bo;
  582. int i, r;
  583. r = amdgpu_vm_update_directories(adev, vm);
  584. if (r)
  585. return r;
  586. r = amdgpu_sync_fence(adev, &p->job->sync, vm->last_dir_update);
  587. if (r)
  588. return r;
  589. r = amdgpu_vm_clear_freed(adev, vm, NULL);
  590. if (r)
  591. return r;
  592. r = amdgpu_vm_bo_update(adev, fpriv->prt_va, false);
  593. if (r)
  594. return r;
  595. r = amdgpu_sync_fence(adev, &p->job->sync,
  596. fpriv->prt_va->last_pt_update);
  597. if (r)
  598. return r;
  599. if (amdgpu_sriov_vf(adev)) {
  600. struct dma_fence *f;
  601. bo_va = vm->csa_bo_va;
  602. BUG_ON(!bo_va);
  603. r = amdgpu_vm_bo_update(adev, bo_va, false);
  604. if (r)
  605. return r;
  606. f = bo_va->last_pt_update;
  607. r = amdgpu_sync_fence(adev, &p->job->sync, f);
  608. if (r)
  609. return r;
  610. }
  611. if (p->bo_list) {
  612. for (i = 0; i < p->bo_list->num_entries; i++) {
  613. struct dma_fence *f;
  614. /* ignore duplicates */
  615. bo = p->bo_list->array[i].robj;
  616. if (!bo)
  617. continue;
  618. bo_va = p->bo_list->array[i].bo_va;
  619. if (bo_va == NULL)
  620. continue;
  621. r = amdgpu_vm_bo_update(adev, bo_va, false);
  622. if (r)
  623. return r;
  624. f = bo_va->last_pt_update;
  625. r = amdgpu_sync_fence(adev, &p->job->sync, f);
  626. if (r)
  627. return r;
  628. }
  629. }
  630. r = amdgpu_vm_clear_invalids(adev, vm, &p->job->sync);
  631. if (amdgpu_vm_debug && p->bo_list) {
  632. /* Invalidate all BOs to test for userspace bugs */
  633. for (i = 0; i < p->bo_list->num_entries; i++) {
  634. /* ignore duplicates */
  635. bo = p->bo_list->array[i].robj;
  636. if (!bo)
  637. continue;
  638. amdgpu_vm_bo_invalidate(adev, bo);
  639. }
  640. }
  641. return r;
  642. }
  643. static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
  644. struct amdgpu_cs_parser *p)
  645. {
  646. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  647. struct amdgpu_vm *vm = &fpriv->vm;
  648. struct amdgpu_ring *ring = p->job->ring;
  649. int i, r;
  650. /* Only for UVD/VCE VM emulation */
  651. if (ring->funcs->parse_cs) {
  652. for (i = 0; i < p->job->num_ibs; i++) {
  653. r = amdgpu_ring_parse_cs(ring, p, i);
  654. if (r)
  655. return r;
  656. }
  657. }
  658. if (p->job->vm) {
  659. p->job->vm_pd_addr = amdgpu_bo_gpu_offset(vm->root.bo);
  660. r = amdgpu_bo_vm_update_pte(p);
  661. if (r)
  662. return r;
  663. }
  664. return amdgpu_cs_sync_rings(p);
  665. }
  666. static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
  667. struct amdgpu_cs_parser *parser)
  668. {
  669. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  670. struct amdgpu_vm *vm = &fpriv->vm;
  671. int i, j;
  672. int r, ce_preempt = 0, de_preempt = 0;
  673. for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) {
  674. struct amdgpu_cs_chunk *chunk;
  675. struct amdgpu_ib *ib;
  676. struct drm_amdgpu_cs_chunk_ib *chunk_ib;
  677. struct amdgpu_ring *ring;
  678. chunk = &parser->chunks[i];
  679. ib = &parser->job->ibs[j];
  680. chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
  681. if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
  682. continue;
  683. if (chunk_ib->ip_type == AMDGPU_HW_IP_GFX && amdgpu_sriov_vf(adev)) {
  684. if (chunk_ib->flags & AMDGPU_IB_FLAG_PREEMPT) {
  685. if (chunk_ib->flags & AMDGPU_IB_FLAG_CE)
  686. ce_preempt++;
  687. else
  688. de_preempt++;
  689. }
  690. /* each GFX command submit allows 0 or 1 IB preemptible for CE & DE */
  691. if (ce_preempt > 1 || de_preempt > 1)
  692. return -EINVAL;
  693. }
  694. r = amdgpu_queue_mgr_map(adev, &parser->ctx->queue_mgr, chunk_ib->ip_type,
  695. chunk_ib->ip_instance, chunk_ib->ring, &ring);
  696. if (r)
  697. return r;
  698. if (chunk_ib->flags & AMDGPU_IB_FLAG_PREAMBLE) {
  699. parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT;
  700. if (!parser->ctx->preamble_presented) {
  701. parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT_FIRST;
  702. parser->ctx->preamble_presented = true;
  703. }
  704. }
  705. if (parser->job->ring && parser->job->ring != ring)
  706. return -EINVAL;
  707. parser->job->ring = ring;
  708. if (ring->funcs->parse_cs) {
  709. struct amdgpu_bo_va_mapping *m;
  710. struct amdgpu_bo *aobj = NULL;
  711. uint64_t offset;
  712. uint8_t *kptr;
  713. m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
  714. &aobj);
  715. if (!aobj) {
  716. DRM_ERROR("IB va_start is invalid\n");
  717. return -EINVAL;
  718. }
  719. if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
  720. (m->last + 1) * AMDGPU_GPU_PAGE_SIZE) {
  721. DRM_ERROR("IB va_start+ib_bytes is invalid\n");
  722. return -EINVAL;
  723. }
  724. /* the IB should be reserved at this point */
  725. r = amdgpu_bo_kmap(aobj, (void **)&kptr);
  726. if (r) {
  727. return r;
  728. }
  729. offset = m->start * AMDGPU_GPU_PAGE_SIZE;
  730. kptr += chunk_ib->va_start - offset;
  731. r = amdgpu_ib_get(adev, vm, chunk_ib->ib_bytes, ib);
  732. if (r) {
  733. DRM_ERROR("Failed to get ib !\n");
  734. return r;
  735. }
  736. memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
  737. amdgpu_bo_kunmap(aobj);
  738. } else {
  739. r = amdgpu_ib_get(adev, vm, 0, ib);
  740. if (r) {
  741. DRM_ERROR("Failed to get ib !\n");
  742. return r;
  743. }
  744. }
  745. ib->gpu_addr = chunk_ib->va_start;
  746. ib->length_dw = chunk_ib->ib_bytes / 4;
  747. ib->flags = chunk_ib->flags;
  748. j++;
  749. }
  750. /* UVD & VCE fw doesn't support user fences */
  751. if (parser->job->uf_addr && (
  752. parser->job->ring->funcs->type == AMDGPU_RING_TYPE_UVD ||
  753. parser->job->ring->funcs->type == AMDGPU_RING_TYPE_VCE))
  754. return -EINVAL;
  755. return 0;
  756. }
  757. static int amdgpu_cs_process_fence_dep(struct amdgpu_cs_parser *p,
  758. struct amdgpu_cs_chunk *chunk)
  759. {
  760. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  761. unsigned num_deps;
  762. int i, r;
  763. struct drm_amdgpu_cs_chunk_dep *deps;
  764. deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
  765. num_deps = chunk->length_dw * 4 /
  766. sizeof(struct drm_amdgpu_cs_chunk_dep);
  767. for (i = 0; i < num_deps; ++i) {
  768. struct amdgpu_ring *ring;
  769. struct amdgpu_ctx *ctx;
  770. struct dma_fence *fence;
  771. ctx = amdgpu_ctx_get(fpriv, deps[i].ctx_id);
  772. if (ctx == NULL)
  773. return -EINVAL;
  774. r = amdgpu_queue_mgr_map(p->adev, &ctx->queue_mgr,
  775. deps[i].ip_type,
  776. deps[i].ip_instance,
  777. deps[i].ring, &ring);
  778. if (r) {
  779. amdgpu_ctx_put(ctx);
  780. return r;
  781. }
  782. fence = amdgpu_ctx_get_fence(ctx, ring,
  783. deps[i].handle);
  784. if (IS_ERR(fence)) {
  785. r = PTR_ERR(fence);
  786. amdgpu_ctx_put(ctx);
  787. return r;
  788. } else if (fence) {
  789. r = amdgpu_sync_fence(p->adev, &p->job->sync,
  790. fence);
  791. dma_fence_put(fence);
  792. amdgpu_ctx_put(ctx);
  793. if (r)
  794. return r;
  795. }
  796. }
  797. return 0;
  798. }
  799. static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
  800. struct amdgpu_cs_parser *p)
  801. {
  802. int i, r;
  803. for (i = 0; i < p->nchunks; ++i) {
  804. struct amdgpu_cs_chunk *chunk;
  805. chunk = &p->chunks[i];
  806. if (chunk->chunk_id == AMDGPU_CHUNK_ID_DEPENDENCIES) {
  807. r = amdgpu_cs_process_fence_dep(p, chunk);
  808. if (r)
  809. return r;
  810. }
  811. }
  812. return 0;
  813. }
  814. static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
  815. union drm_amdgpu_cs *cs)
  816. {
  817. struct amdgpu_ring *ring = p->job->ring;
  818. struct amd_sched_entity *entity = &p->ctx->rings[ring->idx].entity;
  819. struct amdgpu_job *job;
  820. int r;
  821. job = p->job;
  822. p->job = NULL;
  823. r = amd_sched_job_init(&job->base, &ring->sched, entity, p->filp);
  824. if (r) {
  825. amdgpu_job_free(job);
  826. return r;
  827. }
  828. job->owner = p->filp;
  829. job->fence_ctx = entity->fence_context;
  830. p->fence = dma_fence_get(&job->base.s_fence->finished);
  831. cs->out.handle = amdgpu_ctx_add_fence(p->ctx, ring, p->fence);
  832. job->uf_sequence = cs->out.handle;
  833. amdgpu_job_free_resources(job);
  834. amdgpu_cs_parser_fini(p, 0, true);
  835. trace_amdgpu_cs_ioctl(job);
  836. amd_sched_entity_push_job(&job->base);
  837. return 0;
  838. }
  839. int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  840. {
  841. struct amdgpu_device *adev = dev->dev_private;
  842. struct amdgpu_fpriv *fpriv = filp->driver_priv;
  843. union drm_amdgpu_cs *cs = data;
  844. struct amdgpu_cs_parser parser = {};
  845. bool reserved_buffers = false;
  846. int i, r;
  847. if (!adev->accel_working)
  848. return -EBUSY;
  849. if (amdgpu_kms_vram_lost(adev, fpriv))
  850. return -ENODEV;
  851. parser.adev = adev;
  852. parser.filp = filp;
  853. r = amdgpu_cs_parser_init(&parser, data);
  854. if (r) {
  855. DRM_ERROR("Failed to initialize parser !\n");
  856. goto out;
  857. }
  858. r = amdgpu_cs_parser_bos(&parser, data);
  859. if (r) {
  860. if (r == -ENOMEM)
  861. DRM_ERROR("Not enough memory for command submission!\n");
  862. else if (r != -ERESTARTSYS)
  863. DRM_ERROR("Failed to process the buffer list %d!\n", r);
  864. goto out;
  865. }
  866. reserved_buffers = true;
  867. r = amdgpu_cs_ib_fill(adev, &parser);
  868. if (r)
  869. goto out;
  870. r = amdgpu_cs_dependencies(adev, &parser);
  871. if (r) {
  872. DRM_ERROR("Failed in the dependencies handling %d!\n", r);
  873. goto out;
  874. }
  875. for (i = 0; i < parser.job->num_ibs; i++)
  876. trace_amdgpu_cs(&parser, i);
  877. r = amdgpu_cs_ib_vm_chunk(adev, &parser);
  878. if (r)
  879. goto out;
  880. r = amdgpu_cs_submit(&parser, cs);
  881. if (r)
  882. goto out;
  883. return 0;
  884. out:
  885. amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
  886. return r;
  887. }
  888. /**
  889. * amdgpu_cs_wait_ioctl - wait for a command submission to finish
  890. *
  891. * @dev: drm device
  892. * @data: data from userspace
  893. * @filp: file private
  894. *
  895. * Wait for the command submission identified by handle to finish.
  896. */
  897. int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
  898. struct drm_file *filp)
  899. {
  900. union drm_amdgpu_wait_cs *wait = data;
  901. struct amdgpu_device *adev = dev->dev_private;
  902. struct amdgpu_fpriv *fpriv = filp->driver_priv;
  903. unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
  904. struct amdgpu_ring *ring = NULL;
  905. struct amdgpu_ctx *ctx;
  906. struct dma_fence *fence;
  907. long r;
  908. if (amdgpu_kms_vram_lost(adev, fpriv))
  909. return -ENODEV;
  910. ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
  911. if (ctx == NULL)
  912. return -EINVAL;
  913. r = amdgpu_queue_mgr_map(adev, &ctx->queue_mgr,
  914. wait->in.ip_type, wait->in.ip_instance,
  915. wait->in.ring, &ring);
  916. if (r) {
  917. amdgpu_ctx_put(ctx);
  918. return r;
  919. }
  920. fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
  921. if (IS_ERR(fence))
  922. r = PTR_ERR(fence);
  923. else if (fence) {
  924. r = dma_fence_wait_timeout(fence, true, timeout);
  925. dma_fence_put(fence);
  926. } else
  927. r = 1;
  928. amdgpu_ctx_put(ctx);
  929. if (r < 0)
  930. return r;
  931. memset(wait, 0, sizeof(*wait));
  932. wait->out.status = (r == 0);
  933. return 0;
  934. }
  935. /**
  936. * amdgpu_cs_get_fence - helper to get fence from drm_amdgpu_fence
  937. *
  938. * @adev: amdgpu device
  939. * @filp: file private
  940. * @user: drm_amdgpu_fence copied from user space
  941. */
  942. static struct dma_fence *amdgpu_cs_get_fence(struct amdgpu_device *adev,
  943. struct drm_file *filp,
  944. struct drm_amdgpu_fence *user)
  945. {
  946. struct amdgpu_ring *ring;
  947. struct amdgpu_ctx *ctx;
  948. struct dma_fence *fence;
  949. int r;
  950. ctx = amdgpu_ctx_get(filp->driver_priv, user->ctx_id);
  951. if (ctx == NULL)
  952. return ERR_PTR(-EINVAL);
  953. r = amdgpu_queue_mgr_map(adev, &ctx->queue_mgr, user->ip_type,
  954. user->ip_instance, user->ring, &ring);
  955. if (r) {
  956. amdgpu_ctx_put(ctx);
  957. return ERR_PTR(r);
  958. }
  959. fence = amdgpu_ctx_get_fence(ctx, ring, user->seq_no);
  960. amdgpu_ctx_put(ctx);
  961. return fence;
  962. }
  963. /**
  964. * amdgpu_cs_wait_all_fence - wait on all fences to signal
  965. *
  966. * @adev: amdgpu device
  967. * @filp: file private
  968. * @wait: wait parameters
  969. * @fences: array of drm_amdgpu_fence
  970. */
  971. static int amdgpu_cs_wait_all_fences(struct amdgpu_device *adev,
  972. struct drm_file *filp,
  973. union drm_amdgpu_wait_fences *wait,
  974. struct drm_amdgpu_fence *fences)
  975. {
  976. uint32_t fence_count = wait->in.fence_count;
  977. unsigned int i;
  978. long r = 1;
  979. for (i = 0; i < fence_count; i++) {
  980. struct dma_fence *fence;
  981. unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
  982. fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
  983. if (IS_ERR(fence))
  984. return PTR_ERR(fence);
  985. else if (!fence)
  986. continue;
  987. r = dma_fence_wait_timeout(fence, true, timeout);
  988. dma_fence_put(fence);
  989. if (r < 0)
  990. return r;
  991. if (r == 0)
  992. break;
  993. }
  994. memset(wait, 0, sizeof(*wait));
  995. wait->out.status = (r > 0);
  996. return 0;
  997. }
  998. /**
  999. * amdgpu_cs_wait_any_fence - wait on any fence to signal
  1000. *
  1001. * @adev: amdgpu device
  1002. * @filp: file private
  1003. * @wait: wait parameters
  1004. * @fences: array of drm_amdgpu_fence
  1005. */
  1006. static int amdgpu_cs_wait_any_fence(struct amdgpu_device *adev,
  1007. struct drm_file *filp,
  1008. union drm_amdgpu_wait_fences *wait,
  1009. struct drm_amdgpu_fence *fences)
  1010. {
  1011. unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
  1012. uint32_t fence_count = wait->in.fence_count;
  1013. uint32_t first = ~0;
  1014. struct dma_fence **array;
  1015. unsigned int i;
  1016. long r;
  1017. /* Prepare the fence array */
  1018. array = kcalloc(fence_count, sizeof(struct dma_fence *), GFP_KERNEL);
  1019. if (array == NULL)
  1020. return -ENOMEM;
  1021. for (i = 0; i < fence_count; i++) {
  1022. struct dma_fence *fence;
  1023. fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
  1024. if (IS_ERR(fence)) {
  1025. r = PTR_ERR(fence);
  1026. goto err_free_fence_array;
  1027. } else if (fence) {
  1028. array[i] = fence;
  1029. } else { /* NULL, the fence has been already signaled */
  1030. r = 1;
  1031. goto out;
  1032. }
  1033. }
  1034. r = dma_fence_wait_any_timeout(array, fence_count, true, timeout,
  1035. &first);
  1036. if (r < 0)
  1037. goto err_free_fence_array;
  1038. out:
  1039. memset(wait, 0, sizeof(*wait));
  1040. wait->out.status = (r > 0);
  1041. wait->out.first_signaled = first;
  1042. /* set return value 0 to indicate success */
  1043. r = 0;
  1044. err_free_fence_array:
  1045. for (i = 0; i < fence_count; i++)
  1046. dma_fence_put(array[i]);
  1047. kfree(array);
  1048. return r;
  1049. }
  1050. /**
  1051. * amdgpu_cs_wait_fences_ioctl - wait for multiple command submissions to finish
  1052. *
  1053. * @dev: drm device
  1054. * @data: data from userspace
  1055. * @filp: file private
  1056. */
  1057. int amdgpu_cs_wait_fences_ioctl(struct drm_device *dev, void *data,
  1058. struct drm_file *filp)
  1059. {
  1060. struct amdgpu_device *adev = dev->dev_private;
  1061. struct amdgpu_fpriv *fpriv = filp->driver_priv;
  1062. union drm_amdgpu_wait_fences *wait = data;
  1063. uint32_t fence_count = wait->in.fence_count;
  1064. struct drm_amdgpu_fence *fences_user;
  1065. struct drm_amdgpu_fence *fences;
  1066. int r;
  1067. if (amdgpu_kms_vram_lost(adev, fpriv))
  1068. return -ENODEV;
  1069. /* Get the fences from userspace */
  1070. fences = kmalloc_array(fence_count, sizeof(struct drm_amdgpu_fence),
  1071. GFP_KERNEL);
  1072. if (fences == NULL)
  1073. return -ENOMEM;
  1074. fences_user = (void __user *)(uintptr_t)(wait->in.fences);
  1075. if (copy_from_user(fences, fences_user,
  1076. sizeof(struct drm_amdgpu_fence) * fence_count)) {
  1077. r = -EFAULT;
  1078. goto err_free_fences;
  1079. }
  1080. if (wait->in.wait_all)
  1081. r = amdgpu_cs_wait_all_fences(adev, filp, wait, fences);
  1082. else
  1083. r = amdgpu_cs_wait_any_fence(adev, filp, wait, fences);
  1084. err_free_fences:
  1085. kfree(fences);
  1086. return r;
  1087. }
  1088. /**
  1089. * amdgpu_cs_find_bo_va - find bo_va for VM address
  1090. *
  1091. * @parser: command submission parser context
  1092. * @addr: VM address
  1093. * @bo: resulting BO of the mapping found
  1094. *
  1095. * Search the buffer objects in the command submission context for a certain
  1096. * virtual memory address. Returns allocation structure when found, NULL
  1097. * otherwise.
  1098. */
  1099. struct amdgpu_bo_va_mapping *
  1100. amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
  1101. uint64_t addr, struct amdgpu_bo **bo)
  1102. {
  1103. struct amdgpu_bo_va_mapping *mapping;
  1104. unsigned i;
  1105. if (!parser->bo_list)
  1106. return NULL;
  1107. addr /= AMDGPU_GPU_PAGE_SIZE;
  1108. for (i = 0; i < parser->bo_list->num_entries; i++) {
  1109. struct amdgpu_bo_list_entry *lobj;
  1110. lobj = &parser->bo_list->array[i];
  1111. if (!lobj->bo_va)
  1112. continue;
  1113. list_for_each_entry(mapping, &lobj->bo_va->valids, list) {
  1114. if (mapping->start > addr ||
  1115. addr > mapping->last)
  1116. continue;
  1117. *bo = lobj->bo_va->bo;
  1118. return mapping;
  1119. }
  1120. list_for_each_entry(mapping, &lobj->bo_va->invalids, list) {
  1121. if (mapping->start > addr ||
  1122. addr > mapping->last)
  1123. continue;
  1124. *bo = lobj->bo_va->bo;
  1125. return mapping;
  1126. }
  1127. }
  1128. return NULL;
  1129. }
  1130. /**
  1131. * amdgpu_cs_sysvm_access_required - make BOs accessible by the system VM
  1132. *
  1133. * @parser: command submission parser context
  1134. *
  1135. * Helper for UVD/VCE VM emulation, make sure BOs are accessible by the system VM.
  1136. */
  1137. int amdgpu_cs_sysvm_access_required(struct amdgpu_cs_parser *parser)
  1138. {
  1139. unsigned i;
  1140. int r;
  1141. if (!parser->bo_list)
  1142. return 0;
  1143. for (i = 0; i < parser->bo_list->num_entries; i++) {
  1144. struct amdgpu_bo *bo = parser->bo_list->array[i].robj;
  1145. r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem);
  1146. if (unlikely(r))
  1147. return r;
  1148. if (bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)
  1149. continue;
  1150. bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
  1151. amdgpu_ttm_placement_from_domain(bo, bo->allowed_domains);
  1152. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
  1153. if (unlikely(r))
  1154. return r;
  1155. }
  1156. return 0;
  1157. }