amdgpu_cs.c 39 KB

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