amdgpu_cs.c 34 KB

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