amdgpu_cs.c 40 KB

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