amdgpu_cs.c 39 KB

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