amdgpu_cs.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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)) {
  326. if (r != -ERESTARTSYS && domain != bo->allowed_domains) {
  327. domain = bo->allowed_domains;
  328. goto retry;
  329. }
  330. }
  331. return r;
  332. }
  333. int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p,
  334. struct list_head *validated)
  335. {
  336. struct amdgpu_bo_list_entry *lobj;
  337. int r;
  338. list_for_each_entry(lobj, validated, tv.head) {
  339. struct amdgpu_bo *bo = lobj->robj;
  340. bool binding_userptr = false;
  341. struct mm_struct *usermm;
  342. usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
  343. if (usermm && usermm != current->mm)
  344. return -EPERM;
  345. /* Check if we have user pages and nobody bound the BO already */
  346. if (lobj->user_pages && bo->tbo.ttm->state != tt_bound) {
  347. size_t size = sizeof(struct page *);
  348. size *= bo->tbo.ttm->num_pages;
  349. memcpy(bo->tbo.ttm->pages, lobj->user_pages, size);
  350. binding_userptr = true;
  351. }
  352. r = amdgpu_cs_bo_validate(p, bo);
  353. if (r)
  354. return r;
  355. if (bo->shadow) {
  356. r = amdgpu_cs_bo_validate(p, bo);
  357. if (r)
  358. return r;
  359. }
  360. if (binding_userptr) {
  361. drm_free_large(lobj->user_pages);
  362. lobj->user_pages = NULL;
  363. }
  364. }
  365. return 0;
  366. }
  367. static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
  368. union drm_amdgpu_cs *cs)
  369. {
  370. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  371. struct amdgpu_bo_list_entry *e;
  372. struct list_head duplicates;
  373. bool need_mmap_lock = false;
  374. unsigned i, tries = 10;
  375. int r;
  376. INIT_LIST_HEAD(&p->validated);
  377. p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
  378. if (p->bo_list) {
  379. need_mmap_lock = p->bo_list->first_userptr !=
  380. p->bo_list->num_entries;
  381. amdgpu_bo_list_get_list(p->bo_list, &p->validated);
  382. }
  383. INIT_LIST_HEAD(&duplicates);
  384. amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
  385. if (p->uf_entry.robj)
  386. list_add(&p->uf_entry.tv.head, &p->validated);
  387. if (need_mmap_lock)
  388. down_read(&current->mm->mmap_sem);
  389. while (1) {
  390. struct list_head need_pages;
  391. unsigned i;
  392. r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
  393. &duplicates);
  394. if (unlikely(r != 0)) {
  395. DRM_ERROR("ttm_eu_reserve_buffers failed.\n");
  396. goto error_free_pages;
  397. }
  398. /* Without a BO list we don't have userptr BOs */
  399. if (!p->bo_list)
  400. break;
  401. INIT_LIST_HEAD(&need_pages);
  402. for (i = p->bo_list->first_userptr;
  403. i < p->bo_list->num_entries; ++i) {
  404. e = &p->bo_list->array[i];
  405. if (amdgpu_ttm_tt_userptr_invalidated(e->robj->tbo.ttm,
  406. &e->user_invalidated) && e->user_pages) {
  407. /* We acquired a page array, but somebody
  408. * invalidated it. Free it an try again
  409. */
  410. release_pages(e->user_pages,
  411. e->robj->tbo.ttm->num_pages,
  412. false);
  413. drm_free_large(e->user_pages);
  414. e->user_pages = NULL;
  415. }
  416. if (e->robj->tbo.ttm->state != tt_bound &&
  417. !e->user_pages) {
  418. list_del(&e->tv.head);
  419. list_add(&e->tv.head, &need_pages);
  420. amdgpu_bo_unreserve(e->robj);
  421. }
  422. }
  423. if (list_empty(&need_pages))
  424. break;
  425. /* Unreserve everything again. */
  426. ttm_eu_backoff_reservation(&p->ticket, &p->validated);
  427. /* We tried too many times, just abort */
  428. if (!--tries) {
  429. r = -EDEADLK;
  430. DRM_ERROR("deadlock in %s\n", __func__);
  431. goto error_free_pages;
  432. }
  433. /* Fill the page arrays for all useptrs. */
  434. list_for_each_entry(e, &need_pages, tv.head) {
  435. struct ttm_tt *ttm = e->robj->tbo.ttm;
  436. e->user_pages = drm_calloc_large(ttm->num_pages,
  437. sizeof(struct page*));
  438. if (!e->user_pages) {
  439. r = -ENOMEM;
  440. DRM_ERROR("calloc failure in %s\n", __func__);
  441. goto error_free_pages;
  442. }
  443. r = amdgpu_ttm_tt_get_user_pages(ttm, e->user_pages);
  444. if (r) {
  445. DRM_ERROR("amdgpu_ttm_tt_get_user_pages failed.\n");
  446. drm_free_large(e->user_pages);
  447. e->user_pages = NULL;
  448. goto error_free_pages;
  449. }
  450. }
  451. /* And try again. */
  452. list_splice(&need_pages, &p->validated);
  453. }
  454. amdgpu_vm_get_pt_bos(p->adev, &fpriv->vm, &duplicates);
  455. p->bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(p->adev);
  456. p->bytes_moved = 0;
  457. r = amdgpu_cs_list_validate(p, &duplicates);
  458. if (r) {
  459. DRM_ERROR("amdgpu_cs_list_validate(duplicates) failed.\n");
  460. goto error_validate;
  461. }
  462. r = amdgpu_cs_list_validate(p, &p->validated);
  463. if (r) {
  464. DRM_ERROR("amdgpu_cs_list_validate(validated) failed.\n");
  465. goto error_validate;
  466. }
  467. amdgpu_cs_report_moved_bytes(p->adev, p->bytes_moved);
  468. fpriv->vm.last_eviction_counter =
  469. atomic64_read(&p->adev->num_evictions);
  470. if (p->bo_list) {
  471. struct amdgpu_bo *gds = p->bo_list->gds_obj;
  472. struct amdgpu_bo *gws = p->bo_list->gws_obj;
  473. struct amdgpu_bo *oa = p->bo_list->oa_obj;
  474. struct amdgpu_vm *vm = &fpriv->vm;
  475. unsigned i;
  476. for (i = 0; i < p->bo_list->num_entries; i++) {
  477. struct amdgpu_bo *bo = p->bo_list->array[i].robj;
  478. p->bo_list->array[i].bo_va = amdgpu_vm_bo_find(vm, bo);
  479. }
  480. if (gds) {
  481. p->job->gds_base = amdgpu_bo_gpu_offset(gds);
  482. p->job->gds_size = amdgpu_bo_size(gds);
  483. }
  484. if (gws) {
  485. p->job->gws_base = amdgpu_bo_gpu_offset(gws);
  486. p->job->gws_size = amdgpu_bo_size(gws);
  487. }
  488. if (oa) {
  489. p->job->oa_base = amdgpu_bo_gpu_offset(oa);
  490. p->job->oa_size = amdgpu_bo_size(oa);
  491. }
  492. }
  493. if (p->uf_entry.robj)
  494. p->job->uf_addr += amdgpu_bo_gpu_offset(p->uf_entry.robj);
  495. error_validate:
  496. if (r) {
  497. amdgpu_vm_move_pt_bos_in_lru(p->adev, &fpriv->vm);
  498. ttm_eu_backoff_reservation(&p->ticket, &p->validated);
  499. }
  500. error_free_pages:
  501. if (need_mmap_lock)
  502. up_read(&current->mm->mmap_sem);
  503. if (p->bo_list) {
  504. for (i = p->bo_list->first_userptr;
  505. i < p->bo_list->num_entries; ++i) {
  506. e = &p->bo_list->array[i];
  507. if (!e->user_pages)
  508. continue;
  509. release_pages(e->user_pages,
  510. e->robj->tbo.ttm->num_pages,
  511. false);
  512. drm_free_large(e->user_pages);
  513. }
  514. }
  515. return r;
  516. }
  517. static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
  518. {
  519. struct amdgpu_bo_list_entry *e;
  520. int r;
  521. list_for_each_entry(e, &p->validated, tv.head) {
  522. struct reservation_object *resv = e->robj->tbo.resv;
  523. r = amdgpu_sync_resv(p->adev, &p->job->sync, resv, p->filp);
  524. if (r)
  525. return r;
  526. }
  527. return 0;
  528. }
  529. /**
  530. * cs_parser_fini() - clean parser states
  531. * @parser: parser structure holding parsing context.
  532. * @error: error number
  533. *
  534. * If error is set than unvalidate buffer, otherwise just free memory
  535. * used by parsing context.
  536. **/
  537. static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
  538. {
  539. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  540. unsigned i;
  541. if (!error) {
  542. amdgpu_vm_move_pt_bos_in_lru(parser->adev, &fpriv->vm);
  543. ttm_eu_fence_buffer_objects(&parser->ticket,
  544. &parser->validated,
  545. parser->fence);
  546. } else if (backoff) {
  547. ttm_eu_backoff_reservation(&parser->ticket,
  548. &parser->validated);
  549. }
  550. fence_put(parser->fence);
  551. if (parser->ctx)
  552. amdgpu_ctx_put(parser->ctx);
  553. if (parser->bo_list)
  554. amdgpu_bo_list_put(parser->bo_list);
  555. for (i = 0; i < parser->nchunks; i++)
  556. drm_free_large(parser->chunks[i].kdata);
  557. kfree(parser->chunks);
  558. if (parser->job)
  559. amdgpu_job_free(parser->job);
  560. amdgpu_bo_unref(&parser->uf_entry.robj);
  561. }
  562. static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p,
  563. struct amdgpu_vm *vm)
  564. {
  565. struct amdgpu_device *adev = p->adev;
  566. struct amdgpu_bo_va *bo_va;
  567. struct amdgpu_bo *bo;
  568. int i, r;
  569. r = amdgpu_vm_update_page_directory(adev, vm);
  570. if (r)
  571. return r;
  572. r = amdgpu_sync_fence(adev, &p->job->sync, vm->page_directory_fence);
  573. if (r)
  574. return r;
  575. r = amdgpu_vm_clear_freed(adev, vm);
  576. if (r)
  577. return r;
  578. if (p->bo_list) {
  579. for (i = 0; i < p->bo_list->num_entries; i++) {
  580. struct fence *f;
  581. /* ignore duplicates */
  582. bo = p->bo_list->array[i].robj;
  583. if (!bo)
  584. continue;
  585. bo_va = p->bo_list->array[i].bo_va;
  586. if (bo_va == NULL)
  587. continue;
  588. r = amdgpu_vm_bo_update(adev, bo_va, false);
  589. if (r)
  590. return r;
  591. f = bo_va->last_pt_update;
  592. r = amdgpu_sync_fence(adev, &p->job->sync, f);
  593. if (r)
  594. return r;
  595. }
  596. }
  597. r = amdgpu_vm_clear_invalids(adev, vm, &p->job->sync);
  598. if (amdgpu_vm_debug && p->bo_list) {
  599. /* Invalidate all BOs to test for userspace bugs */
  600. for (i = 0; i < p->bo_list->num_entries; i++) {
  601. /* ignore duplicates */
  602. bo = p->bo_list->array[i].robj;
  603. if (!bo)
  604. continue;
  605. amdgpu_vm_bo_invalidate(adev, bo);
  606. }
  607. }
  608. return r;
  609. }
  610. static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
  611. struct amdgpu_cs_parser *p)
  612. {
  613. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  614. struct amdgpu_vm *vm = &fpriv->vm;
  615. struct amdgpu_ring *ring = p->job->ring;
  616. int i, r;
  617. /* Only for UVD/VCE VM emulation */
  618. if (ring->funcs->parse_cs) {
  619. p->job->vm = NULL;
  620. for (i = 0; i < p->job->num_ibs; i++) {
  621. r = amdgpu_ring_parse_cs(ring, p, i);
  622. if (r)
  623. return r;
  624. }
  625. } else {
  626. p->job->vm_pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
  627. r = amdgpu_bo_vm_update_pte(p, vm);
  628. if (r)
  629. return r;
  630. }
  631. return amdgpu_cs_sync_rings(p);
  632. }
  633. static int amdgpu_cs_handle_lockup(struct amdgpu_device *adev, int r)
  634. {
  635. if (r == -EDEADLK) {
  636. r = amdgpu_gpu_reset(adev);
  637. if (!r)
  638. r = -EAGAIN;
  639. }
  640. return r;
  641. }
  642. static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
  643. struct amdgpu_cs_parser *parser)
  644. {
  645. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  646. struct amdgpu_vm *vm = &fpriv->vm;
  647. int i, j;
  648. int r;
  649. for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) {
  650. struct amdgpu_cs_chunk *chunk;
  651. struct amdgpu_ib *ib;
  652. struct drm_amdgpu_cs_chunk_ib *chunk_ib;
  653. struct amdgpu_ring *ring;
  654. chunk = &parser->chunks[i];
  655. ib = &parser->job->ibs[j];
  656. chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
  657. if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
  658. continue;
  659. r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
  660. chunk_ib->ip_instance, chunk_ib->ring,
  661. &ring);
  662. if (r)
  663. return r;
  664. if (parser->job->ring && parser->job->ring != ring)
  665. return -EINVAL;
  666. parser->job->ring = ring;
  667. if (ring->funcs->parse_cs) {
  668. struct amdgpu_bo_va_mapping *m;
  669. struct amdgpu_bo *aobj = NULL;
  670. uint64_t offset;
  671. uint8_t *kptr;
  672. m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
  673. &aobj);
  674. if (!aobj) {
  675. DRM_ERROR("IB va_start is invalid\n");
  676. return -EINVAL;
  677. }
  678. if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
  679. (m->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
  680. DRM_ERROR("IB va_start+ib_bytes is invalid\n");
  681. return -EINVAL;
  682. }
  683. /* the IB should be reserved at this point */
  684. r = amdgpu_bo_kmap(aobj, (void **)&kptr);
  685. if (r) {
  686. return r;
  687. }
  688. offset = ((uint64_t)m->it.start) * AMDGPU_GPU_PAGE_SIZE;
  689. kptr += chunk_ib->va_start - offset;
  690. r = amdgpu_ib_get(adev, NULL, chunk_ib->ib_bytes, ib);
  691. if (r) {
  692. DRM_ERROR("Failed to get ib !\n");
  693. return r;
  694. }
  695. memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
  696. amdgpu_bo_kunmap(aobj);
  697. } else {
  698. r = amdgpu_ib_get(adev, vm, 0, ib);
  699. if (r) {
  700. DRM_ERROR("Failed to get ib !\n");
  701. return r;
  702. }
  703. ib->gpu_addr = chunk_ib->va_start;
  704. }
  705. ib->length_dw = chunk_ib->ib_bytes / 4;
  706. ib->flags = chunk_ib->flags;
  707. j++;
  708. }
  709. /* UVD & VCE fw doesn't support user fences */
  710. if (parser->job->uf_addr && (
  711. parser->job->ring->type == AMDGPU_RING_TYPE_UVD ||
  712. parser->job->ring->type == AMDGPU_RING_TYPE_VCE))
  713. return -EINVAL;
  714. return 0;
  715. }
  716. static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
  717. struct amdgpu_cs_parser *p)
  718. {
  719. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  720. int i, j, r;
  721. for (i = 0; i < p->nchunks; ++i) {
  722. struct drm_amdgpu_cs_chunk_dep *deps;
  723. struct amdgpu_cs_chunk *chunk;
  724. unsigned num_deps;
  725. chunk = &p->chunks[i];
  726. if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES)
  727. continue;
  728. deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
  729. num_deps = chunk->length_dw * 4 /
  730. sizeof(struct drm_amdgpu_cs_chunk_dep);
  731. for (j = 0; j < num_deps; ++j) {
  732. struct amdgpu_ring *ring;
  733. struct amdgpu_ctx *ctx;
  734. struct fence *fence;
  735. r = amdgpu_cs_get_ring(adev, deps[j].ip_type,
  736. deps[j].ip_instance,
  737. deps[j].ring, &ring);
  738. if (r)
  739. return r;
  740. ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id);
  741. if (ctx == NULL)
  742. return -EINVAL;
  743. fence = amdgpu_ctx_get_fence(ctx, ring,
  744. deps[j].handle);
  745. if (IS_ERR(fence)) {
  746. r = PTR_ERR(fence);
  747. amdgpu_ctx_put(ctx);
  748. return r;
  749. } else if (fence) {
  750. r = amdgpu_sync_fence(adev, &p->job->sync,
  751. fence);
  752. fence_put(fence);
  753. amdgpu_ctx_put(ctx);
  754. if (r)
  755. return r;
  756. }
  757. }
  758. }
  759. return 0;
  760. }
  761. static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
  762. union drm_amdgpu_cs *cs)
  763. {
  764. struct amdgpu_ring *ring = p->job->ring;
  765. struct amd_sched_entity *entity = &p->ctx->rings[ring->idx].entity;
  766. struct amdgpu_job *job;
  767. int r;
  768. job = p->job;
  769. p->job = NULL;
  770. r = amd_sched_job_init(&job->base, &ring->sched, entity, p->filp);
  771. if (r) {
  772. amdgpu_job_free(job);
  773. return r;
  774. }
  775. job->owner = p->filp;
  776. job->ctx = entity->fence_context;
  777. p->fence = fence_get(&job->base.s_fence->finished);
  778. cs->out.handle = amdgpu_ctx_add_fence(p->ctx, ring, p->fence);
  779. job->uf_sequence = cs->out.handle;
  780. amdgpu_job_free_resources(job);
  781. trace_amdgpu_cs_ioctl(job);
  782. amd_sched_entity_push_job(&job->base);
  783. return 0;
  784. }
  785. int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  786. {
  787. struct amdgpu_device *adev = dev->dev_private;
  788. union drm_amdgpu_cs *cs = data;
  789. struct amdgpu_cs_parser parser = {};
  790. bool reserved_buffers = false;
  791. int i, r;
  792. if (!adev->accel_working)
  793. return -EBUSY;
  794. parser.adev = adev;
  795. parser.filp = filp;
  796. r = amdgpu_cs_parser_init(&parser, data);
  797. if (r) {
  798. DRM_ERROR("Failed to initialize parser !\n");
  799. amdgpu_cs_parser_fini(&parser, r, false);
  800. r = amdgpu_cs_handle_lockup(adev, r);
  801. return r;
  802. }
  803. r = amdgpu_cs_parser_bos(&parser, data);
  804. if (r == -ENOMEM)
  805. DRM_ERROR("Not enough memory for command submission!\n");
  806. else if (r && r != -ERESTARTSYS)
  807. DRM_ERROR("Failed to process the buffer list %d!\n", r);
  808. else if (!r) {
  809. reserved_buffers = true;
  810. r = amdgpu_cs_ib_fill(adev, &parser);
  811. }
  812. if (!r) {
  813. r = amdgpu_cs_dependencies(adev, &parser);
  814. if (r)
  815. DRM_ERROR("Failed in the dependencies handling %d!\n", r);
  816. }
  817. if (r)
  818. goto out;
  819. for (i = 0; i < parser.job->num_ibs; i++)
  820. trace_amdgpu_cs(&parser, i);
  821. r = amdgpu_cs_ib_vm_chunk(adev, &parser);
  822. if (r)
  823. goto out;
  824. r = amdgpu_cs_submit(&parser, cs);
  825. out:
  826. amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
  827. r = amdgpu_cs_handle_lockup(adev, r);
  828. return r;
  829. }
  830. /**
  831. * amdgpu_cs_wait_ioctl - wait for a command submission to finish
  832. *
  833. * @dev: drm device
  834. * @data: data from userspace
  835. * @filp: file private
  836. *
  837. * Wait for the command submission identified by handle to finish.
  838. */
  839. int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
  840. struct drm_file *filp)
  841. {
  842. union drm_amdgpu_wait_cs *wait = data;
  843. struct amdgpu_device *adev = dev->dev_private;
  844. unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
  845. struct amdgpu_ring *ring = NULL;
  846. struct amdgpu_ctx *ctx;
  847. struct fence *fence;
  848. long r;
  849. r = amdgpu_cs_get_ring(adev, wait->in.ip_type, wait->in.ip_instance,
  850. wait->in.ring, &ring);
  851. if (r)
  852. return r;
  853. ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
  854. if (ctx == NULL)
  855. return -EINVAL;
  856. fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
  857. if (IS_ERR(fence))
  858. r = PTR_ERR(fence);
  859. else if (fence) {
  860. r = fence_wait_timeout(fence, true, timeout);
  861. fence_put(fence);
  862. } else
  863. r = 1;
  864. amdgpu_ctx_put(ctx);
  865. if (r < 0)
  866. return r;
  867. memset(wait, 0, sizeof(*wait));
  868. wait->out.status = (r == 0);
  869. return 0;
  870. }
  871. /**
  872. * amdgpu_cs_find_bo_va - find bo_va for VM address
  873. *
  874. * @parser: command submission parser context
  875. * @addr: VM address
  876. * @bo: resulting BO of the mapping found
  877. *
  878. * Search the buffer objects in the command submission context for a certain
  879. * virtual memory address. Returns allocation structure when found, NULL
  880. * otherwise.
  881. */
  882. struct amdgpu_bo_va_mapping *
  883. amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
  884. uint64_t addr, struct amdgpu_bo **bo)
  885. {
  886. struct amdgpu_bo_va_mapping *mapping;
  887. unsigned i;
  888. if (!parser->bo_list)
  889. return NULL;
  890. addr /= AMDGPU_GPU_PAGE_SIZE;
  891. for (i = 0; i < parser->bo_list->num_entries; i++) {
  892. struct amdgpu_bo_list_entry *lobj;
  893. lobj = &parser->bo_list->array[i];
  894. if (!lobj->bo_va)
  895. continue;
  896. list_for_each_entry(mapping, &lobj->bo_va->valids, list) {
  897. if (mapping->it.start > addr ||
  898. addr > mapping->it.last)
  899. continue;
  900. *bo = lobj->bo_va->bo;
  901. return mapping;
  902. }
  903. list_for_each_entry(mapping, &lobj->bo_va->invalids, list) {
  904. if (mapping->it.start > addr ||
  905. addr > mapping->it.last)
  906. continue;
  907. *bo = lobj->bo_va->bo;
  908. return mapping;
  909. }
  910. }
  911. return NULL;
  912. }