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