amdgpu_cs.c 38 KB

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