amdgpu_cs.c 39 KB

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