amdgpu_cs.c 39 KB

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