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