amdgpu_cs.c 29 KB

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