amdgpu_cs.c 39 KB

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