amdgpu_cs.c 34 KB

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