amdgpu_cs.c 34 KB

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