amdgpu_cs.c 40 KB

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