amdgpu_cs.c 35 KB

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