amdgpu_cs.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  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 <drm/drmP.h>
  29. #include <drm/amdgpu_drm.h>
  30. #include "amdgpu.h"
  31. #include "amdgpu_trace.h"
  32. int amdgpu_cs_get_ring(struct amdgpu_device *adev, u32 ip_type,
  33. u32 ip_instance, u32 ring,
  34. struct amdgpu_ring **out_ring)
  35. {
  36. /* Right now all IPs have only one instance - multiple rings. */
  37. if (ip_instance != 0) {
  38. DRM_ERROR("invalid ip instance: %d\n", ip_instance);
  39. return -EINVAL;
  40. }
  41. switch (ip_type) {
  42. default:
  43. DRM_ERROR("unknown ip type: %d\n", ip_type);
  44. return -EINVAL;
  45. case AMDGPU_HW_IP_GFX:
  46. if (ring < adev->gfx.num_gfx_rings) {
  47. *out_ring = &adev->gfx.gfx_ring[ring];
  48. } else {
  49. DRM_ERROR("only %d gfx rings are supported now\n",
  50. adev->gfx.num_gfx_rings);
  51. return -EINVAL;
  52. }
  53. break;
  54. case AMDGPU_HW_IP_COMPUTE:
  55. if (ring < adev->gfx.num_compute_rings) {
  56. *out_ring = &adev->gfx.compute_ring[ring];
  57. } else {
  58. DRM_ERROR("only %d compute rings are supported now\n",
  59. adev->gfx.num_compute_rings);
  60. return -EINVAL;
  61. }
  62. break;
  63. case AMDGPU_HW_IP_DMA:
  64. if (ring < adev->sdma.num_instances) {
  65. *out_ring = &adev->sdma.instance[ring].ring;
  66. } else {
  67. DRM_ERROR("only %d SDMA rings are supported\n",
  68. adev->sdma.num_instances);
  69. return -EINVAL;
  70. }
  71. break;
  72. case AMDGPU_HW_IP_UVD:
  73. *out_ring = &adev->uvd.ring;
  74. break;
  75. case AMDGPU_HW_IP_VCE:
  76. if (ring < 2){
  77. *out_ring = &adev->vce.ring[ring];
  78. } else {
  79. DRM_ERROR("only two VCE rings are supported\n");
  80. return -EINVAL;
  81. }
  82. break;
  83. }
  84. return 0;
  85. }
  86. static int amdgpu_cs_user_fence_chunk(struct amdgpu_cs_parser *p,
  87. struct drm_amdgpu_cs_chunk_fence *data,
  88. uint32_t *offset)
  89. {
  90. struct drm_gem_object *gobj;
  91. gobj = drm_gem_object_lookup(p->filp, data->handle);
  92. if (gobj == NULL)
  93. return -EINVAL;
  94. p->uf_entry.robj = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
  95. p->uf_entry.priority = 0;
  96. p->uf_entry.tv.bo = &p->uf_entry.robj->tbo;
  97. p->uf_entry.tv.shared = true;
  98. p->uf_entry.user_pages = NULL;
  99. *offset = data->offset;
  100. drm_gem_object_unreference_unlocked(gobj);
  101. if (amdgpu_ttm_tt_get_usermm(p->uf_entry.robj->tbo.ttm)) {
  102. amdgpu_bo_unref(&p->uf_entry.robj);
  103. return -EINVAL;
  104. }
  105. return 0;
  106. }
  107. int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, void *data)
  108. {
  109. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  110. struct amdgpu_vm *vm = &fpriv->vm;
  111. union drm_amdgpu_cs *cs = data;
  112. uint64_t *chunk_array_user;
  113. uint64_t *chunk_array;
  114. unsigned size, num_ibs = 0;
  115. uint32_t uf_offset = 0;
  116. int i;
  117. int ret;
  118. if (cs->in.num_chunks == 0)
  119. return 0;
  120. chunk_array = kmalloc_array(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL);
  121. if (!chunk_array)
  122. return -ENOMEM;
  123. p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id);
  124. if (!p->ctx) {
  125. ret = -EINVAL;
  126. goto free_chunk;
  127. }
  128. /* get chunks */
  129. chunk_array_user = (uint64_t __user *)(unsigned long)(cs->in.chunks);
  130. if (copy_from_user(chunk_array, chunk_array_user,
  131. sizeof(uint64_t)*cs->in.num_chunks)) {
  132. ret = -EFAULT;
  133. goto put_ctx;
  134. }
  135. p->nchunks = cs->in.num_chunks;
  136. p->chunks = kmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk),
  137. GFP_KERNEL);
  138. if (!p->chunks) {
  139. ret = -ENOMEM;
  140. goto put_ctx;
  141. }
  142. for (i = 0; i < p->nchunks; i++) {
  143. struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL;
  144. struct drm_amdgpu_cs_chunk user_chunk;
  145. uint32_t __user *cdata;
  146. chunk_ptr = (void __user *)(unsigned long)chunk_array[i];
  147. if (copy_from_user(&user_chunk, chunk_ptr,
  148. sizeof(struct drm_amdgpu_cs_chunk))) {
  149. ret = -EFAULT;
  150. i--;
  151. goto free_partial_kdata;
  152. }
  153. p->chunks[i].chunk_id = user_chunk.chunk_id;
  154. p->chunks[i].length_dw = user_chunk.length_dw;
  155. size = p->chunks[i].length_dw;
  156. cdata = (void __user *)(unsigned long)user_chunk.chunk_data;
  157. p->chunks[i].kdata = drm_malloc_ab(size, sizeof(uint32_t));
  158. if (p->chunks[i].kdata == NULL) {
  159. ret = -ENOMEM;
  160. i--;
  161. goto free_partial_kdata;
  162. }
  163. size *= sizeof(uint32_t);
  164. if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
  165. ret = -EFAULT;
  166. goto free_partial_kdata;
  167. }
  168. switch (p->chunks[i].chunk_id) {
  169. case AMDGPU_CHUNK_ID_IB:
  170. ++num_ibs;
  171. break;
  172. case AMDGPU_CHUNK_ID_FENCE:
  173. size = sizeof(struct drm_amdgpu_cs_chunk_fence);
  174. if (p->chunks[i].length_dw * sizeof(uint32_t) < size) {
  175. ret = -EINVAL;
  176. goto free_partial_kdata;
  177. }
  178. ret = amdgpu_cs_user_fence_chunk(p, p->chunks[i].kdata,
  179. &uf_offset);
  180. if (ret)
  181. goto free_partial_kdata;
  182. break;
  183. case AMDGPU_CHUNK_ID_DEPENDENCIES:
  184. break;
  185. default:
  186. ret = -EINVAL;
  187. goto free_partial_kdata;
  188. }
  189. }
  190. ret = amdgpu_job_alloc(p->adev, num_ibs, &p->job, vm);
  191. if (ret)
  192. goto free_all_kdata;
  193. if (p->uf_entry.robj)
  194. p->job->uf_addr = uf_offset;
  195. kfree(chunk_array);
  196. return 0;
  197. free_all_kdata:
  198. i = p->nchunks - 1;
  199. free_partial_kdata:
  200. for (; i >= 0; i--)
  201. drm_free_large(p->chunks[i].kdata);
  202. kfree(p->chunks);
  203. put_ctx:
  204. amdgpu_ctx_put(p->ctx);
  205. free_chunk:
  206. kfree(chunk_array);
  207. return ret;
  208. }
  209. /* Convert microseconds to bytes. */
  210. static u64 us_to_bytes(struct amdgpu_device *adev, s64 us)
  211. {
  212. if (us <= 0 || !adev->mm_stats.log2_max_MBps)
  213. return 0;
  214. /* Since accum_us is incremented by a million per second, just
  215. * multiply it by the number of MB/s to get the number of bytes.
  216. */
  217. return us << adev->mm_stats.log2_max_MBps;
  218. }
  219. static s64 bytes_to_us(struct amdgpu_device *adev, u64 bytes)
  220. {
  221. if (!adev->mm_stats.log2_max_MBps)
  222. return 0;
  223. return bytes >> adev->mm_stats.log2_max_MBps;
  224. }
  225. /* Returns how many bytes TTM can move right now. If no bytes can be moved,
  226. * it returns 0. If it returns non-zero, it's OK to move at least one buffer,
  227. * which means it can go over the threshold once. If that happens, the driver
  228. * will be in debt and no other buffer migrations can be done until that debt
  229. * is repaid.
  230. *
  231. * This approach allows moving a buffer of any size (it's important to allow
  232. * that).
  233. *
  234. * The currency is simply time in microseconds and it increases as the clock
  235. * ticks. The accumulated microseconds (us) are converted to bytes and
  236. * returned.
  237. */
  238. static u64 amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev)
  239. {
  240. s64 time_us, increment_us;
  241. u64 max_bytes;
  242. u64 free_vram, total_vram, used_vram;
  243. /* Allow a maximum of 200 accumulated ms. This is basically per-IB
  244. * throttling.
  245. *
  246. * It means that in order to get full max MBps, at least 5 IBs per
  247. * second must be submitted and not more than 200ms apart from each
  248. * other.
  249. */
  250. const s64 us_upper_bound = 200000;
  251. if (!adev->mm_stats.log2_max_MBps)
  252. return 0;
  253. total_vram = adev->mc.real_vram_size - adev->vram_pin_size;
  254. used_vram = atomic64_read(&adev->vram_usage);
  255. free_vram = used_vram >= total_vram ? 0 : total_vram - used_vram;
  256. spin_lock(&adev->mm_stats.lock);
  257. /* Increase the amount of accumulated us. */
  258. time_us = ktime_to_us(ktime_get());
  259. increment_us = time_us - adev->mm_stats.last_update_us;
  260. adev->mm_stats.last_update_us = time_us;
  261. adev->mm_stats.accum_us = min(adev->mm_stats.accum_us + increment_us,
  262. us_upper_bound);
  263. /* This prevents the short period of low performance when the VRAM
  264. * usage is low and the driver is in debt or doesn't have enough
  265. * accumulated us to fill VRAM quickly.
  266. *
  267. * The situation can occur in these cases:
  268. * - a lot of VRAM is freed by userspace
  269. * - the presence of a big buffer causes a lot of evictions
  270. * (solution: split buffers into smaller ones)
  271. *
  272. * If 128 MB or 1/8th of VRAM is free, start filling it now by setting
  273. * accum_us to a positive number.
  274. */
  275. if (free_vram >= 128 * 1024 * 1024 || free_vram >= total_vram / 8) {
  276. s64 min_us;
  277. /* Be more aggresive on dGPUs. Try to fill a portion of free
  278. * VRAM now.
  279. */
  280. if (!(adev->flags & AMD_IS_APU))
  281. min_us = bytes_to_us(adev, free_vram / 4);
  282. else
  283. min_us = 0; /* Reset accum_us on APUs. */
  284. adev->mm_stats.accum_us = max(min_us, adev->mm_stats.accum_us);
  285. }
  286. /* This returns 0 if the driver is in debt to disallow (optional)
  287. * buffer moves.
  288. */
  289. max_bytes = us_to_bytes(adev, adev->mm_stats.accum_us);
  290. spin_unlock(&adev->mm_stats.lock);
  291. return max_bytes;
  292. }
  293. /* Report how many bytes have really been moved for the last command
  294. * submission. This can result in a debt that can stop buffer migrations
  295. * temporarily.
  296. */
  297. static void amdgpu_cs_report_moved_bytes(struct amdgpu_device *adev,
  298. u64 num_bytes)
  299. {
  300. spin_lock(&adev->mm_stats.lock);
  301. adev->mm_stats.accum_us -= bytes_to_us(adev, num_bytes);
  302. spin_unlock(&adev->mm_stats.lock);
  303. }
  304. static int amdgpu_cs_bo_validate(struct amdgpu_cs_parser *p,
  305. struct amdgpu_bo *bo)
  306. {
  307. u64 initial_bytes_moved;
  308. uint32_t domain;
  309. int r;
  310. if (bo->pin_count)
  311. return 0;
  312. /* Don't move this buffer if we have depleted our allowance
  313. * to move it. Don't move anything if the threshold is zero.
  314. */
  315. if (p->bytes_moved < p->bytes_moved_threshold)
  316. domain = bo->prefered_domains;
  317. else
  318. domain = bo->allowed_domains;
  319. retry:
  320. amdgpu_ttm_placement_from_domain(bo, domain);
  321. initial_bytes_moved = atomic64_read(&bo->adev->num_bytes_moved);
  322. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  323. p->bytes_moved += atomic64_read(&bo->adev->num_bytes_moved) -
  324. initial_bytes_moved;
  325. if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) {
  326. domain = bo->allowed_domains;
  327. goto retry;
  328. }
  329. return r;
  330. }
  331. /* Last resort, try to evict something from the current working set */
  332. static bool amdgpu_cs_try_evict(struct amdgpu_cs_parser *p,
  333. struct amdgpu_bo_list_entry *lobj)
  334. {
  335. uint32_t domain = lobj->robj->allowed_domains;
  336. int r;
  337. if (!p->evictable)
  338. return false;
  339. for (;&p->evictable->tv.head != &p->validated;
  340. p->evictable = list_prev_entry(p->evictable, tv.head)) {
  341. struct amdgpu_bo_list_entry *candidate = p->evictable;
  342. struct amdgpu_bo *bo = candidate->robj;
  343. u64 initial_bytes_moved;
  344. uint32_t other;
  345. /* If we reached our current BO we can forget it */
  346. if (candidate == lobj)
  347. break;
  348. other = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
  349. /* Check if this BO is in one of the domains we need space for */
  350. if (!(other & domain))
  351. continue;
  352. /* Check if we can move this BO somewhere else */
  353. other = bo->allowed_domains & ~domain;
  354. if (!other)
  355. continue;
  356. /* Good we can try to move this BO somewhere else */
  357. amdgpu_ttm_placement_from_domain(bo, other);
  358. initial_bytes_moved = atomic64_read(&bo->adev->num_bytes_moved);
  359. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  360. p->bytes_moved += atomic64_read(&bo->adev->num_bytes_moved) -
  361. initial_bytes_moved;
  362. if (unlikely(r))
  363. break;
  364. p->evictable = list_prev_entry(p->evictable, tv.head);
  365. list_move(&candidate->tv.head, &p->validated);
  366. return true;
  367. }
  368. return false;
  369. }
  370. int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p,
  371. struct list_head *validated)
  372. {
  373. struct amdgpu_bo_list_entry *lobj;
  374. int r;
  375. list_for_each_entry(lobj, validated, tv.head) {
  376. struct amdgpu_bo *bo = lobj->robj;
  377. bool binding_userptr = false;
  378. struct mm_struct *usermm;
  379. usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
  380. if (usermm && usermm != current->mm)
  381. return -EPERM;
  382. /* Check if we have user pages and nobody bound the BO already */
  383. if (lobj->user_pages && bo->tbo.ttm->state != tt_bound) {
  384. size_t size = sizeof(struct page *);
  385. size *= bo->tbo.ttm->num_pages;
  386. memcpy(bo->tbo.ttm->pages, lobj->user_pages, size);
  387. binding_userptr = true;
  388. }
  389. if (p->evictable == lobj)
  390. p->evictable = NULL;
  391. do {
  392. r = amdgpu_cs_bo_validate(p, bo);
  393. } while (r == -ENOMEM && amdgpu_cs_try_evict(p, lobj));
  394. if (r)
  395. return r;
  396. if (bo->shadow) {
  397. r = amdgpu_cs_bo_validate(p, bo);
  398. if (r)
  399. return r;
  400. }
  401. if (binding_userptr) {
  402. drm_free_large(lobj->user_pages);
  403. lobj->user_pages = NULL;
  404. }
  405. }
  406. return 0;
  407. }
  408. static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
  409. union drm_amdgpu_cs *cs)
  410. {
  411. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  412. struct amdgpu_bo_list_entry *e;
  413. struct list_head duplicates;
  414. bool need_mmap_lock = false;
  415. unsigned i, tries = 10;
  416. int r;
  417. INIT_LIST_HEAD(&p->validated);
  418. p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
  419. if (p->bo_list) {
  420. need_mmap_lock = p->bo_list->first_userptr !=
  421. p->bo_list->num_entries;
  422. amdgpu_bo_list_get_list(p->bo_list, &p->validated);
  423. }
  424. INIT_LIST_HEAD(&duplicates);
  425. amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
  426. if (p->uf_entry.robj)
  427. list_add(&p->uf_entry.tv.head, &p->validated);
  428. if (need_mmap_lock)
  429. down_read(&current->mm->mmap_sem);
  430. while (1) {
  431. struct list_head need_pages;
  432. unsigned i;
  433. r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
  434. &duplicates);
  435. if (unlikely(r != 0)) {
  436. DRM_ERROR("ttm_eu_reserve_buffers failed.\n");
  437. goto error_free_pages;
  438. }
  439. /* Without a BO list we don't have userptr BOs */
  440. if (!p->bo_list)
  441. break;
  442. INIT_LIST_HEAD(&need_pages);
  443. for (i = p->bo_list->first_userptr;
  444. i < p->bo_list->num_entries; ++i) {
  445. e = &p->bo_list->array[i];
  446. if (amdgpu_ttm_tt_userptr_invalidated(e->robj->tbo.ttm,
  447. &e->user_invalidated) && e->user_pages) {
  448. /* We acquired a page array, but somebody
  449. * invalidated it. Free it an try again
  450. */
  451. release_pages(e->user_pages,
  452. e->robj->tbo.ttm->num_pages,
  453. false);
  454. drm_free_large(e->user_pages);
  455. e->user_pages = NULL;
  456. }
  457. if (e->robj->tbo.ttm->state != tt_bound &&
  458. !e->user_pages) {
  459. list_del(&e->tv.head);
  460. list_add(&e->tv.head, &need_pages);
  461. amdgpu_bo_unreserve(e->robj);
  462. }
  463. }
  464. if (list_empty(&need_pages))
  465. break;
  466. /* Unreserve everything again. */
  467. ttm_eu_backoff_reservation(&p->ticket, &p->validated);
  468. /* We tried too many times, just abort */
  469. if (!--tries) {
  470. r = -EDEADLK;
  471. DRM_ERROR("deadlock in %s\n", __func__);
  472. goto error_free_pages;
  473. }
  474. /* Fill the page arrays for all useptrs. */
  475. list_for_each_entry(e, &need_pages, tv.head) {
  476. struct ttm_tt *ttm = e->robj->tbo.ttm;
  477. e->user_pages = drm_calloc_large(ttm->num_pages,
  478. sizeof(struct page*));
  479. if (!e->user_pages) {
  480. r = -ENOMEM;
  481. DRM_ERROR("calloc failure in %s\n", __func__);
  482. goto error_free_pages;
  483. }
  484. r = amdgpu_ttm_tt_get_user_pages(ttm, e->user_pages);
  485. if (r) {
  486. DRM_ERROR("amdgpu_ttm_tt_get_user_pages failed.\n");
  487. drm_free_large(e->user_pages);
  488. e->user_pages = NULL;
  489. goto error_free_pages;
  490. }
  491. }
  492. /* And try again. */
  493. list_splice(&need_pages, &p->validated);
  494. }
  495. amdgpu_vm_get_pt_bos(p->adev, &fpriv->vm, &duplicates);
  496. p->bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(p->adev);
  497. p->bytes_moved = 0;
  498. p->evictable = list_last_entry(&p->validated,
  499. struct amdgpu_bo_list_entry,
  500. tv.head);
  501. r = amdgpu_cs_list_validate(p, &duplicates);
  502. if (r) {
  503. DRM_ERROR("amdgpu_cs_list_validate(duplicates) failed.\n");
  504. goto error_validate;
  505. }
  506. r = amdgpu_cs_list_validate(p, &p->validated);
  507. if (r) {
  508. DRM_ERROR("amdgpu_cs_list_validate(validated) failed.\n");
  509. goto error_validate;
  510. }
  511. amdgpu_cs_report_moved_bytes(p->adev, p->bytes_moved);
  512. fpriv->vm.last_eviction_counter =
  513. atomic64_read(&p->adev->num_evictions);
  514. if (p->bo_list) {
  515. struct amdgpu_bo *gds = p->bo_list->gds_obj;
  516. struct amdgpu_bo *gws = p->bo_list->gws_obj;
  517. struct amdgpu_bo *oa = p->bo_list->oa_obj;
  518. struct amdgpu_vm *vm = &fpriv->vm;
  519. unsigned i;
  520. for (i = 0; i < p->bo_list->num_entries; i++) {
  521. struct amdgpu_bo *bo = p->bo_list->array[i].robj;
  522. p->bo_list->array[i].bo_va = amdgpu_vm_bo_find(vm, bo);
  523. }
  524. if (gds) {
  525. p->job->gds_base = amdgpu_bo_gpu_offset(gds);
  526. p->job->gds_size = amdgpu_bo_size(gds);
  527. }
  528. if (gws) {
  529. p->job->gws_base = amdgpu_bo_gpu_offset(gws);
  530. p->job->gws_size = amdgpu_bo_size(gws);
  531. }
  532. if (oa) {
  533. p->job->oa_base = amdgpu_bo_gpu_offset(oa);
  534. p->job->oa_size = amdgpu_bo_size(oa);
  535. }
  536. }
  537. if (!r && p->uf_entry.robj) {
  538. struct amdgpu_bo *uf = p->uf_entry.robj;
  539. r = amdgpu_ttm_bind(uf->tbo.ttm, &uf->tbo.mem);
  540. p->job->uf_addr += amdgpu_bo_gpu_offset(uf);
  541. }
  542. error_validate:
  543. if (r) {
  544. amdgpu_vm_move_pt_bos_in_lru(p->adev, &fpriv->vm);
  545. ttm_eu_backoff_reservation(&p->ticket, &p->validated);
  546. }
  547. error_free_pages:
  548. if (need_mmap_lock)
  549. up_read(&current->mm->mmap_sem);
  550. if (p->bo_list) {
  551. for (i = p->bo_list->first_userptr;
  552. i < p->bo_list->num_entries; ++i) {
  553. e = &p->bo_list->array[i];
  554. if (!e->user_pages)
  555. continue;
  556. release_pages(e->user_pages,
  557. e->robj->tbo.ttm->num_pages,
  558. false);
  559. drm_free_large(e->user_pages);
  560. }
  561. }
  562. return r;
  563. }
  564. static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
  565. {
  566. struct amdgpu_bo_list_entry *e;
  567. int r;
  568. list_for_each_entry(e, &p->validated, tv.head) {
  569. struct reservation_object *resv = e->robj->tbo.resv;
  570. r = amdgpu_sync_resv(p->adev, &p->job->sync, resv, p->filp);
  571. if (r)
  572. return r;
  573. }
  574. return 0;
  575. }
  576. /**
  577. * cs_parser_fini() - clean parser states
  578. * @parser: parser structure holding parsing context.
  579. * @error: error number
  580. *
  581. * If error is set than unvalidate buffer, otherwise just free memory
  582. * used by parsing context.
  583. **/
  584. static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
  585. {
  586. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  587. unsigned i;
  588. if (!error) {
  589. amdgpu_vm_move_pt_bos_in_lru(parser->adev, &fpriv->vm);
  590. ttm_eu_fence_buffer_objects(&parser->ticket,
  591. &parser->validated,
  592. parser->fence);
  593. } else if (backoff) {
  594. ttm_eu_backoff_reservation(&parser->ticket,
  595. &parser->validated);
  596. }
  597. fence_put(parser->fence);
  598. if (parser->ctx)
  599. amdgpu_ctx_put(parser->ctx);
  600. if (parser->bo_list)
  601. amdgpu_bo_list_put(parser->bo_list);
  602. for (i = 0; i < parser->nchunks; i++)
  603. drm_free_large(parser->chunks[i].kdata);
  604. kfree(parser->chunks);
  605. if (parser->job)
  606. amdgpu_job_free(parser->job);
  607. amdgpu_bo_unref(&parser->uf_entry.robj);
  608. }
  609. static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p,
  610. struct amdgpu_vm *vm)
  611. {
  612. struct amdgpu_device *adev = p->adev;
  613. struct amdgpu_bo_va *bo_va;
  614. struct amdgpu_bo *bo;
  615. int i, r;
  616. r = amdgpu_vm_update_page_directory(adev, vm);
  617. if (r)
  618. return r;
  619. r = amdgpu_sync_fence(adev, &p->job->sync, vm->page_directory_fence);
  620. if (r)
  621. return r;
  622. r = amdgpu_vm_clear_freed(adev, vm);
  623. if (r)
  624. return r;
  625. if (p->bo_list) {
  626. for (i = 0; i < p->bo_list->num_entries; i++) {
  627. struct fence *f;
  628. /* ignore duplicates */
  629. bo = p->bo_list->array[i].robj;
  630. if (!bo)
  631. continue;
  632. bo_va = p->bo_list->array[i].bo_va;
  633. if (bo_va == NULL)
  634. continue;
  635. r = amdgpu_vm_bo_update(adev, bo_va, false);
  636. if (r)
  637. return r;
  638. f = bo_va->last_pt_update;
  639. r = amdgpu_sync_fence(adev, &p->job->sync, f);
  640. if (r)
  641. return r;
  642. }
  643. }
  644. r = amdgpu_vm_clear_invalids(adev, vm, &p->job->sync);
  645. if (amdgpu_vm_debug && p->bo_list) {
  646. /* Invalidate all BOs to test for userspace bugs */
  647. for (i = 0; i < p->bo_list->num_entries; i++) {
  648. /* ignore duplicates */
  649. bo = p->bo_list->array[i].robj;
  650. if (!bo)
  651. continue;
  652. amdgpu_vm_bo_invalidate(adev, bo);
  653. }
  654. }
  655. return r;
  656. }
  657. static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
  658. struct amdgpu_cs_parser *p)
  659. {
  660. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  661. struct amdgpu_vm *vm = &fpriv->vm;
  662. struct amdgpu_ring *ring = p->job->ring;
  663. int i, r;
  664. /* Only for UVD/VCE VM emulation */
  665. if (ring->funcs->parse_cs) {
  666. p->job->vm = NULL;
  667. for (i = 0; i < p->job->num_ibs; i++) {
  668. r = amdgpu_ring_parse_cs(ring, p, i);
  669. if (r)
  670. return r;
  671. }
  672. } else {
  673. p->job->vm_pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
  674. r = amdgpu_bo_vm_update_pte(p, vm);
  675. if (r)
  676. return r;
  677. }
  678. return amdgpu_cs_sync_rings(p);
  679. }
  680. static int amdgpu_cs_handle_lockup(struct amdgpu_device *adev, int r)
  681. {
  682. if (r == -EDEADLK) {
  683. r = amdgpu_gpu_reset(adev);
  684. if (!r)
  685. r = -EAGAIN;
  686. }
  687. return r;
  688. }
  689. static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
  690. struct amdgpu_cs_parser *parser)
  691. {
  692. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  693. struct amdgpu_vm *vm = &fpriv->vm;
  694. int i, j;
  695. int r;
  696. for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) {
  697. struct amdgpu_cs_chunk *chunk;
  698. struct amdgpu_ib *ib;
  699. struct drm_amdgpu_cs_chunk_ib *chunk_ib;
  700. struct amdgpu_ring *ring;
  701. chunk = &parser->chunks[i];
  702. ib = &parser->job->ibs[j];
  703. chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
  704. if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
  705. continue;
  706. r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
  707. chunk_ib->ip_instance, chunk_ib->ring,
  708. &ring);
  709. if (r)
  710. return r;
  711. if (ib->flags & AMDGPU_IB_FLAG_PREAMBLE) {
  712. parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT;
  713. if (!parser->ctx->preamble_presented) {
  714. parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT_FIRST;
  715. parser->ctx->preamble_presented = true;
  716. }
  717. }
  718. if (parser->job->ring && parser->job->ring != ring)
  719. return -EINVAL;
  720. parser->job->ring = ring;
  721. if (ring->funcs->parse_cs) {
  722. struct amdgpu_bo_va_mapping *m;
  723. struct amdgpu_bo *aobj = NULL;
  724. uint64_t offset;
  725. uint8_t *kptr;
  726. m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
  727. &aobj);
  728. if (!aobj) {
  729. DRM_ERROR("IB va_start is invalid\n");
  730. return -EINVAL;
  731. }
  732. if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
  733. (m->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
  734. DRM_ERROR("IB va_start+ib_bytes is invalid\n");
  735. return -EINVAL;
  736. }
  737. /* the IB should be reserved at this point */
  738. r = amdgpu_bo_kmap(aobj, (void **)&kptr);
  739. if (r) {
  740. return r;
  741. }
  742. offset = ((uint64_t)m->it.start) * AMDGPU_GPU_PAGE_SIZE;
  743. kptr += chunk_ib->va_start - offset;
  744. r = amdgpu_ib_get(adev, NULL, chunk_ib->ib_bytes, ib);
  745. if (r) {
  746. DRM_ERROR("Failed to get ib !\n");
  747. return r;
  748. }
  749. memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
  750. amdgpu_bo_kunmap(aobj);
  751. } else {
  752. r = amdgpu_ib_get(adev, vm, 0, ib);
  753. if (r) {
  754. DRM_ERROR("Failed to get ib !\n");
  755. return r;
  756. }
  757. ib->gpu_addr = chunk_ib->va_start;
  758. }
  759. ib->length_dw = chunk_ib->ib_bytes / 4;
  760. ib->flags = chunk_ib->flags;
  761. j++;
  762. }
  763. /* UVD & VCE fw doesn't support user fences */
  764. if (parser->job->uf_addr && (
  765. parser->job->ring->type == AMDGPU_RING_TYPE_UVD ||
  766. parser->job->ring->type == AMDGPU_RING_TYPE_VCE))
  767. return -EINVAL;
  768. return 0;
  769. }
  770. static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
  771. struct amdgpu_cs_parser *p)
  772. {
  773. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  774. int i, j, r;
  775. for (i = 0; i < p->nchunks; ++i) {
  776. struct drm_amdgpu_cs_chunk_dep *deps;
  777. struct amdgpu_cs_chunk *chunk;
  778. unsigned num_deps;
  779. chunk = &p->chunks[i];
  780. if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES)
  781. continue;
  782. deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
  783. num_deps = chunk->length_dw * 4 /
  784. sizeof(struct drm_amdgpu_cs_chunk_dep);
  785. for (j = 0; j < num_deps; ++j) {
  786. struct amdgpu_ring *ring;
  787. struct amdgpu_ctx *ctx;
  788. struct fence *fence;
  789. r = amdgpu_cs_get_ring(adev, deps[j].ip_type,
  790. deps[j].ip_instance,
  791. deps[j].ring, &ring);
  792. if (r)
  793. return r;
  794. ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id);
  795. if (ctx == NULL)
  796. return -EINVAL;
  797. fence = amdgpu_ctx_get_fence(ctx, ring,
  798. deps[j].handle);
  799. if (IS_ERR(fence)) {
  800. r = PTR_ERR(fence);
  801. amdgpu_ctx_put(ctx);
  802. return r;
  803. } else if (fence) {
  804. r = amdgpu_sync_fence(adev, &p->job->sync,
  805. fence);
  806. fence_put(fence);
  807. amdgpu_ctx_put(ctx);
  808. if (r)
  809. return r;
  810. }
  811. }
  812. }
  813. return 0;
  814. }
  815. static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
  816. union drm_amdgpu_cs *cs)
  817. {
  818. struct amdgpu_ring *ring = p->job->ring;
  819. struct amd_sched_entity *entity = &p->ctx->rings[ring->idx].entity;
  820. struct amdgpu_job *job;
  821. int r;
  822. job = p->job;
  823. p->job = NULL;
  824. r = amd_sched_job_init(&job->base, &ring->sched, entity, p->filp);
  825. if (r) {
  826. amdgpu_job_free(job);
  827. return r;
  828. }
  829. job->owner = p->filp;
  830. job->fence_ctx = entity->fence_context;
  831. p->fence = fence_get(&job->base.s_fence->finished);
  832. cs->out.handle = amdgpu_ctx_add_fence(p->ctx, ring, p->fence);
  833. job->uf_sequence = cs->out.handle;
  834. amdgpu_job_free_resources(job);
  835. trace_amdgpu_cs_ioctl(job);
  836. amd_sched_entity_push_job(&job->base);
  837. return 0;
  838. }
  839. int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  840. {
  841. struct amdgpu_device *adev = dev->dev_private;
  842. union drm_amdgpu_cs *cs = data;
  843. struct amdgpu_cs_parser parser = {};
  844. bool reserved_buffers = false;
  845. int i, r;
  846. if (!adev->accel_working)
  847. return -EBUSY;
  848. parser.adev = adev;
  849. parser.filp = filp;
  850. r = amdgpu_cs_parser_init(&parser, data);
  851. if (r) {
  852. DRM_ERROR("Failed to initialize parser !\n");
  853. amdgpu_cs_parser_fini(&parser, r, false);
  854. r = amdgpu_cs_handle_lockup(adev, r);
  855. return r;
  856. }
  857. r = amdgpu_cs_parser_bos(&parser, data);
  858. if (r == -ENOMEM)
  859. DRM_ERROR("Not enough memory for command submission!\n");
  860. else if (r && r != -ERESTARTSYS)
  861. DRM_ERROR("Failed to process the buffer list %d!\n", r);
  862. else if (!r) {
  863. reserved_buffers = true;
  864. r = amdgpu_cs_ib_fill(adev, &parser);
  865. }
  866. if (!r) {
  867. r = amdgpu_cs_dependencies(adev, &parser);
  868. if (r)
  869. DRM_ERROR("Failed in the dependencies handling %d!\n", r);
  870. }
  871. if (r)
  872. goto out;
  873. for (i = 0; i < parser.job->num_ibs; i++)
  874. trace_amdgpu_cs(&parser, i);
  875. r = amdgpu_cs_ib_vm_chunk(adev, &parser);
  876. if (r)
  877. goto out;
  878. r = amdgpu_cs_submit(&parser, cs);
  879. out:
  880. amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
  881. r = amdgpu_cs_handle_lockup(adev, r);
  882. return r;
  883. }
  884. /**
  885. * amdgpu_cs_wait_ioctl - wait for a command submission to finish
  886. *
  887. * @dev: drm device
  888. * @data: data from userspace
  889. * @filp: file private
  890. *
  891. * Wait for the command submission identified by handle to finish.
  892. */
  893. int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
  894. struct drm_file *filp)
  895. {
  896. union drm_amdgpu_wait_cs *wait = data;
  897. struct amdgpu_device *adev = dev->dev_private;
  898. unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
  899. struct amdgpu_ring *ring = NULL;
  900. struct amdgpu_ctx *ctx;
  901. struct fence *fence;
  902. long r;
  903. r = amdgpu_cs_get_ring(adev, wait->in.ip_type, wait->in.ip_instance,
  904. wait->in.ring, &ring);
  905. if (r)
  906. return r;
  907. ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
  908. if (ctx == NULL)
  909. return -EINVAL;
  910. fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
  911. if (IS_ERR(fence))
  912. r = PTR_ERR(fence);
  913. else if (fence) {
  914. r = fence_wait_timeout(fence, true, timeout);
  915. fence_put(fence);
  916. } else
  917. r = 1;
  918. amdgpu_ctx_put(ctx);
  919. if (r < 0)
  920. return r;
  921. memset(wait, 0, sizeof(*wait));
  922. wait->out.status = (r == 0);
  923. return 0;
  924. }
  925. /**
  926. * amdgpu_cs_find_bo_va - find bo_va for VM address
  927. *
  928. * @parser: command submission parser context
  929. * @addr: VM address
  930. * @bo: resulting BO of the mapping found
  931. *
  932. * Search the buffer objects in the command submission context for a certain
  933. * virtual memory address. Returns allocation structure when found, NULL
  934. * otherwise.
  935. */
  936. struct amdgpu_bo_va_mapping *
  937. amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
  938. uint64_t addr, struct amdgpu_bo **bo)
  939. {
  940. struct amdgpu_bo_va_mapping *mapping;
  941. unsigned i;
  942. if (!parser->bo_list)
  943. return NULL;
  944. addr /= AMDGPU_GPU_PAGE_SIZE;
  945. for (i = 0; i < parser->bo_list->num_entries; i++) {
  946. struct amdgpu_bo_list_entry *lobj;
  947. lobj = &parser->bo_list->array[i];
  948. if (!lobj->bo_va)
  949. continue;
  950. list_for_each_entry(mapping, &lobj->bo_va->valids, list) {
  951. if (mapping->it.start > addr ||
  952. addr > mapping->it.last)
  953. continue;
  954. *bo = lobj->bo_va->bo;
  955. return mapping;
  956. }
  957. list_for_each_entry(mapping, &lobj->bo_va->invalids, list) {
  958. if (mapping->it.start > addr ||
  959. addr > mapping->it.last)
  960. continue;
  961. *bo = lobj->bo_va->bo;
  962. return mapping;
  963. }
  964. }
  965. return NULL;
  966. }
  967. /**
  968. * amdgpu_cs_sysvm_access_required - make BOs accessible by the system VM
  969. *
  970. * @parser: command submission parser context
  971. *
  972. * Helper for UVD/VCE VM emulation, make sure BOs are accessible by the system VM.
  973. */
  974. int amdgpu_cs_sysvm_access_required(struct amdgpu_cs_parser *parser)
  975. {
  976. unsigned i;
  977. int r;
  978. if (!parser->bo_list)
  979. return 0;
  980. for (i = 0; i < parser->bo_list->num_entries; i++) {
  981. struct amdgpu_bo *bo = parser->bo_list->array[i].robj;
  982. r = amdgpu_ttm_bind(bo->tbo.ttm, &bo->tbo.mem);
  983. if (unlikely(r))
  984. return r;
  985. }
  986. return 0;
  987. }