amdgpu_cs.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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/list_sort.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 amdgpu_user_fence *uf,
  88. struct drm_amdgpu_cs_chunk_fence *fence_data)
  89. {
  90. struct drm_gem_object *gobj;
  91. uint32_t handle;
  92. handle = fence_data->handle;
  93. gobj = drm_gem_object_lookup(p->adev->ddev, p->filp,
  94. fence_data->handle);
  95. if (gobj == NULL)
  96. return -EINVAL;
  97. uf->bo = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
  98. uf->offset = fence_data->offset;
  99. if (amdgpu_ttm_tt_get_usermm(uf->bo->tbo.ttm)) {
  100. drm_gem_object_unreference_unlocked(gobj);
  101. return -EINVAL;
  102. }
  103. p->uf_entry.robj = amdgpu_bo_ref(uf->bo);
  104. p->uf_entry.priority = 0;
  105. p->uf_entry.tv.bo = &p->uf_entry.robj->tbo;
  106. p->uf_entry.tv.shared = true;
  107. p->uf_entry.user_pages = NULL;
  108. drm_gem_object_unreference_unlocked(gobj);
  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. union drm_amdgpu_cs *cs = data;
  115. uint64_t *chunk_array_user;
  116. uint64_t *chunk_array;
  117. struct amdgpu_user_fence uf = {};
  118. unsigned size, num_ibs = 0;
  119. int i;
  120. int ret;
  121. if (cs->in.num_chunks == 0)
  122. return 0;
  123. chunk_array = kmalloc_array(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL);
  124. if (!chunk_array)
  125. return -ENOMEM;
  126. p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id);
  127. if (!p->ctx) {
  128. ret = -EINVAL;
  129. goto free_chunk;
  130. }
  131. /* get chunks */
  132. chunk_array_user = (uint64_t __user *)(unsigned long)(cs->in.chunks);
  133. if (copy_from_user(chunk_array, chunk_array_user,
  134. sizeof(uint64_t)*cs->in.num_chunks)) {
  135. ret = -EFAULT;
  136. goto put_ctx;
  137. }
  138. p->nchunks = cs->in.num_chunks;
  139. p->chunks = kmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk),
  140. GFP_KERNEL);
  141. if (!p->chunks) {
  142. ret = -ENOMEM;
  143. goto put_ctx;
  144. }
  145. for (i = 0; i < p->nchunks; i++) {
  146. struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL;
  147. struct drm_amdgpu_cs_chunk user_chunk;
  148. uint32_t __user *cdata;
  149. chunk_ptr = (void __user *)(unsigned long)chunk_array[i];
  150. if (copy_from_user(&user_chunk, chunk_ptr,
  151. sizeof(struct drm_amdgpu_cs_chunk))) {
  152. ret = -EFAULT;
  153. i--;
  154. goto free_partial_kdata;
  155. }
  156. p->chunks[i].chunk_id = user_chunk.chunk_id;
  157. p->chunks[i].length_dw = user_chunk.length_dw;
  158. size = p->chunks[i].length_dw;
  159. cdata = (void __user *)(unsigned long)user_chunk.chunk_data;
  160. p->chunks[i].kdata = drm_malloc_ab(size, sizeof(uint32_t));
  161. if (p->chunks[i].kdata == NULL) {
  162. ret = -ENOMEM;
  163. i--;
  164. goto free_partial_kdata;
  165. }
  166. size *= sizeof(uint32_t);
  167. if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
  168. ret = -EFAULT;
  169. goto free_partial_kdata;
  170. }
  171. switch (p->chunks[i].chunk_id) {
  172. case AMDGPU_CHUNK_ID_IB:
  173. ++num_ibs;
  174. break;
  175. case AMDGPU_CHUNK_ID_FENCE:
  176. size = sizeof(struct drm_amdgpu_cs_chunk_fence);
  177. if (p->chunks[i].length_dw * sizeof(uint32_t) < size) {
  178. ret = -EINVAL;
  179. goto free_partial_kdata;
  180. }
  181. ret = amdgpu_cs_user_fence_chunk(p, &uf, (void *)p->chunks[i].kdata);
  182. if (ret)
  183. goto free_partial_kdata;
  184. break;
  185. case AMDGPU_CHUNK_ID_DEPENDENCIES:
  186. break;
  187. default:
  188. ret = -EINVAL;
  189. goto free_partial_kdata;
  190. }
  191. }
  192. ret = amdgpu_job_alloc(p->adev, num_ibs, &p->job);
  193. if (ret)
  194. goto free_all_kdata;
  195. p->job->uf = uf;
  196. kfree(chunk_array);
  197. return 0;
  198. free_all_kdata:
  199. i = p->nchunks - 1;
  200. free_partial_kdata:
  201. for (; i >= 0; i--)
  202. drm_free_large(p->chunks[i].kdata);
  203. kfree(p->chunks);
  204. put_ctx:
  205. amdgpu_ctx_put(p->ctx);
  206. free_chunk:
  207. kfree(chunk_array);
  208. return ret;
  209. }
  210. /* Returns how many bytes TTM can move per IB.
  211. */
  212. static u64 amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev)
  213. {
  214. u64 real_vram_size = adev->mc.real_vram_size;
  215. u64 vram_usage = atomic64_read(&adev->vram_usage);
  216. /* This function is based on the current VRAM usage.
  217. *
  218. * - If all of VRAM is free, allow relocating the number of bytes that
  219. * is equal to 1/4 of the size of VRAM for this IB.
  220. * - If more than one half of VRAM is occupied, only allow relocating
  221. * 1 MB of data for this IB.
  222. *
  223. * - From 0 to one half of used VRAM, the threshold decreases
  224. * linearly.
  225. * __________________
  226. * 1/4 of -|\ |
  227. * VRAM | \ |
  228. * | \ |
  229. * | \ |
  230. * | \ |
  231. * | \ |
  232. * | \ |
  233. * | \________|1 MB
  234. * |----------------|
  235. * VRAM 0 % 100 %
  236. * used used
  237. *
  238. * Note: It's a threshold, not a limit. The threshold must be crossed
  239. * for buffer relocations to stop, so any buffer of an arbitrary size
  240. * can be moved as long as the threshold isn't crossed before
  241. * the relocation takes place. We don't want to disable buffer
  242. * relocations completely.
  243. *
  244. * The idea is that buffers should be placed in VRAM at creation time
  245. * and TTM should only do a minimum number of relocations during
  246. * command submission. In practice, you need to submit at least
  247. * a dozen IBs to move all buffers to VRAM if they are in GTT.
  248. *
  249. * Also, things can get pretty crazy under memory pressure and actual
  250. * VRAM usage can change a lot, so playing safe even at 50% does
  251. * consistently increase performance.
  252. */
  253. u64 half_vram = real_vram_size >> 1;
  254. u64 half_free_vram = vram_usage >= half_vram ? 0 : half_vram - vram_usage;
  255. u64 bytes_moved_threshold = half_free_vram >> 1;
  256. return max(bytes_moved_threshold, 1024*1024ull);
  257. }
  258. int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p,
  259. struct list_head *validated)
  260. {
  261. struct amdgpu_bo_list_entry *lobj;
  262. u64 initial_bytes_moved;
  263. int r;
  264. list_for_each_entry(lobj, validated, tv.head) {
  265. struct amdgpu_bo *bo = lobj->robj;
  266. bool binding_userptr = false;
  267. struct mm_struct *usermm;
  268. uint32_t domain;
  269. usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
  270. if (usermm && usermm != current->mm)
  271. return -EPERM;
  272. /* Check if we have user pages and nobody bound the BO already */
  273. if (lobj->user_pages && bo->tbo.ttm->state != tt_bound) {
  274. size_t size = sizeof(struct page *);
  275. size *= bo->tbo.ttm->num_pages;
  276. memcpy(bo->tbo.ttm->pages, lobj->user_pages, size);
  277. binding_userptr = true;
  278. }
  279. if (bo->pin_count)
  280. continue;
  281. /* Avoid moving this one if we have moved too many buffers
  282. * for this IB already.
  283. *
  284. * Note that this allows moving at least one buffer of
  285. * any size, because it doesn't take the current "bo"
  286. * into account. We don't want to disallow buffer moves
  287. * completely.
  288. */
  289. if (p->bytes_moved <= p->bytes_moved_threshold)
  290. domain = bo->prefered_domains;
  291. else
  292. domain = bo->allowed_domains;
  293. retry:
  294. amdgpu_ttm_placement_from_domain(bo, domain);
  295. initial_bytes_moved = atomic64_read(&bo->adev->num_bytes_moved);
  296. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  297. p->bytes_moved += atomic64_read(&bo->adev->num_bytes_moved) -
  298. initial_bytes_moved;
  299. if (unlikely(r)) {
  300. if (r != -ERESTARTSYS && domain != bo->allowed_domains) {
  301. domain = bo->allowed_domains;
  302. goto retry;
  303. }
  304. return r;
  305. }
  306. if (binding_userptr) {
  307. drm_free_large(lobj->user_pages);
  308. lobj->user_pages = NULL;
  309. }
  310. }
  311. return 0;
  312. }
  313. static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
  314. union drm_amdgpu_cs *cs)
  315. {
  316. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  317. struct amdgpu_bo_list_entry *e;
  318. struct list_head duplicates;
  319. bool need_mmap_lock = false;
  320. unsigned i, tries = 10;
  321. int r;
  322. INIT_LIST_HEAD(&p->validated);
  323. p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
  324. if (p->bo_list) {
  325. need_mmap_lock = p->bo_list->first_userptr !=
  326. p->bo_list->num_entries;
  327. amdgpu_bo_list_get_list(p->bo_list, &p->validated);
  328. }
  329. INIT_LIST_HEAD(&duplicates);
  330. amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
  331. if (p->job->uf.bo)
  332. list_add(&p->uf_entry.tv.head, &p->validated);
  333. if (need_mmap_lock)
  334. down_read(&current->mm->mmap_sem);
  335. while (1) {
  336. struct list_head need_pages;
  337. unsigned i;
  338. r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
  339. &duplicates);
  340. if (unlikely(r != 0))
  341. goto error_free_pages;
  342. /* Without a BO list we don't have userptr BOs */
  343. if (!p->bo_list)
  344. break;
  345. INIT_LIST_HEAD(&need_pages);
  346. for (i = p->bo_list->first_userptr;
  347. i < p->bo_list->num_entries; ++i) {
  348. e = &p->bo_list->array[i];
  349. if (amdgpu_ttm_tt_userptr_invalidated(e->robj->tbo.ttm,
  350. &e->user_invalidated) && e->user_pages) {
  351. /* We acquired a page array, but somebody
  352. * invalidated it. Free it an try again
  353. */
  354. release_pages(e->user_pages,
  355. e->robj->tbo.ttm->num_pages,
  356. false);
  357. drm_free_large(e->user_pages);
  358. e->user_pages = NULL;
  359. }
  360. if (e->robj->tbo.ttm->state != tt_bound &&
  361. !e->user_pages) {
  362. list_del(&e->tv.head);
  363. list_add(&e->tv.head, &need_pages);
  364. amdgpu_bo_unreserve(e->robj);
  365. }
  366. }
  367. if (list_empty(&need_pages))
  368. break;
  369. /* Unreserve everything again. */
  370. ttm_eu_backoff_reservation(&p->ticket, &p->validated);
  371. /* We tried to often, just abort */
  372. if (!--tries) {
  373. r = -EDEADLK;
  374. goto error_free_pages;
  375. }
  376. /* Fill the page arrays for all useptrs. */
  377. list_for_each_entry(e, &need_pages, tv.head) {
  378. struct ttm_tt *ttm = e->robj->tbo.ttm;
  379. e->user_pages = drm_calloc_large(ttm->num_pages,
  380. sizeof(struct page*));
  381. if (!e->user_pages) {
  382. r = -ENOMEM;
  383. goto error_free_pages;
  384. }
  385. r = amdgpu_ttm_tt_get_user_pages(ttm, e->user_pages);
  386. if (r) {
  387. drm_free_large(e->user_pages);
  388. e->user_pages = NULL;
  389. goto error_free_pages;
  390. }
  391. }
  392. /* And try again. */
  393. list_splice(&need_pages, &p->validated);
  394. }
  395. amdgpu_vm_get_pt_bos(&fpriv->vm, &duplicates);
  396. p->bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(p->adev);
  397. p->bytes_moved = 0;
  398. r = amdgpu_cs_list_validate(p, &duplicates);
  399. if (r)
  400. goto error_validate;
  401. r = amdgpu_cs_list_validate(p, &p->validated);
  402. if (r)
  403. goto error_validate;
  404. if (p->bo_list) {
  405. struct amdgpu_vm *vm = &fpriv->vm;
  406. unsigned i;
  407. for (i = 0; i < p->bo_list->num_entries; i++) {
  408. struct amdgpu_bo *bo = p->bo_list->array[i].robj;
  409. p->bo_list->array[i].bo_va = amdgpu_vm_bo_find(vm, bo);
  410. }
  411. }
  412. error_validate:
  413. if (r) {
  414. amdgpu_vm_move_pt_bos_in_lru(p->adev, &fpriv->vm);
  415. ttm_eu_backoff_reservation(&p->ticket, &p->validated);
  416. }
  417. error_free_pages:
  418. if (need_mmap_lock)
  419. up_read(&current->mm->mmap_sem);
  420. if (p->bo_list) {
  421. for (i = p->bo_list->first_userptr;
  422. i < p->bo_list->num_entries; ++i) {
  423. e = &p->bo_list->array[i];
  424. if (!e->user_pages)
  425. continue;
  426. release_pages(e->user_pages,
  427. e->robj->tbo.ttm->num_pages,
  428. false);
  429. drm_free_large(e->user_pages);
  430. }
  431. }
  432. return r;
  433. }
  434. static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
  435. {
  436. struct amdgpu_bo_list_entry *e;
  437. int r;
  438. list_for_each_entry(e, &p->validated, tv.head) {
  439. struct reservation_object *resv = e->robj->tbo.resv;
  440. r = amdgpu_sync_resv(p->adev, &p->job->sync, resv, p->filp);
  441. if (r)
  442. return r;
  443. }
  444. return 0;
  445. }
  446. static int cmp_size_smaller_first(void *priv, struct list_head *a,
  447. struct list_head *b)
  448. {
  449. struct amdgpu_bo_list_entry *la = list_entry(a, struct amdgpu_bo_list_entry, tv.head);
  450. struct amdgpu_bo_list_entry *lb = list_entry(b, struct amdgpu_bo_list_entry, tv.head);
  451. /* Sort A before B if A is smaller. */
  452. return (int)la->robj->tbo.num_pages - (int)lb->robj->tbo.num_pages;
  453. }
  454. /**
  455. * cs_parser_fini() - clean parser states
  456. * @parser: parser structure holding parsing context.
  457. * @error: error number
  458. *
  459. * If error is set than unvalidate buffer, otherwise just free memory
  460. * used by parsing context.
  461. **/
  462. static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
  463. {
  464. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  465. unsigned i;
  466. if (!error) {
  467. amdgpu_vm_move_pt_bos_in_lru(parser->adev, &fpriv->vm);
  468. /* Sort the buffer list from the smallest to largest buffer,
  469. * which affects the order of buffers in the LRU list.
  470. * This assures that the smallest buffers are added first
  471. * to the LRU list, so they are likely to be later evicted
  472. * first, instead of large buffers whose eviction is more
  473. * expensive.
  474. *
  475. * This slightly lowers the number of bytes moved by TTM
  476. * per frame under memory pressure.
  477. */
  478. list_sort(NULL, &parser->validated, cmp_size_smaller_first);
  479. ttm_eu_fence_buffer_objects(&parser->ticket,
  480. &parser->validated,
  481. parser->fence);
  482. } else if (backoff) {
  483. ttm_eu_backoff_reservation(&parser->ticket,
  484. &parser->validated);
  485. }
  486. fence_put(parser->fence);
  487. if (parser->ctx)
  488. amdgpu_ctx_put(parser->ctx);
  489. if (parser->bo_list)
  490. amdgpu_bo_list_put(parser->bo_list);
  491. for (i = 0; i < parser->nchunks; i++)
  492. drm_free_large(parser->chunks[i].kdata);
  493. kfree(parser->chunks);
  494. if (parser->job)
  495. amdgpu_job_free(parser->job);
  496. amdgpu_bo_unref(&parser->uf_entry.robj);
  497. }
  498. static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p,
  499. struct amdgpu_vm *vm)
  500. {
  501. struct amdgpu_device *adev = p->adev;
  502. struct amdgpu_bo_va *bo_va;
  503. struct amdgpu_bo *bo;
  504. int i, r;
  505. r = amdgpu_vm_update_page_directory(adev, vm);
  506. if (r)
  507. return r;
  508. r = amdgpu_sync_fence(adev, &p->job->sync, vm->page_directory_fence);
  509. if (r)
  510. return r;
  511. r = amdgpu_vm_clear_freed(adev, vm);
  512. if (r)
  513. return r;
  514. if (p->bo_list) {
  515. for (i = 0; i < p->bo_list->num_entries; i++) {
  516. struct fence *f;
  517. /* ignore duplicates */
  518. bo = p->bo_list->array[i].robj;
  519. if (!bo)
  520. continue;
  521. bo_va = p->bo_list->array[i].bo_va;
  522. if (bo_va == NULL)
  523. continue;
  524. r = amdgpu_vm_bo_update(adev, bo_va, &bo->tbo.mem);
  525. if (r)
  526. return r;
  527. f = bo_va->last_pt_update;
  528. r = amdgpu_sync_fence(adev, &p->job->sync, f);
  529. if (r)
  530. return r;
  531. }
  532. }
  533. r = amdgpu_vm_clear_invalids(adev, vm, &p->job->sync);
  534. if (amdgpu_vm_debug && p->bo_list) {
  535. /* Invalidate all BOs to test for userspace bugs */
  536. for (i = 0; i < p->bo_list->num_entries; i++) {
  537. /* ignore duplicates */
  538. bo = p->bo_list->array[i].robj;
  539. if (!bo)
  540. continue;
  541. amdgpu_vm_bo_invalidate(adev, bo);
  542. }
  543. }
  544. return r;
  545. }
  546. static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
  547. struct amdgpu_cs_parser *p)
  548. {
  549. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  550. struct amdgpu_vm *vm = &fpriv->vm;
  551. struct amdgpu_ring *ring = p->job->ring;
  552. int i, r;
  553. /* Only for UVD/VCE VM emulation */
  554. if (ring->funcs->parse_cs) {
  555. for (i = 0; i < p->job->num_ibs; i++) {
  556. r = amdgpu_ring_parse_cs(ring, p, i);
  557. if (r)
  558. return r;
  559. }
  560. }
  561. r = amdgpu_bo_vm_update_pte(p, vm);
  562. if (!r)
  563. amdgpu_cs_sync_rings(p);
  564. return r;
  565. }
  566. static int amdgpu_cs_handle_lockup(struct amdgpu_device *adev, int r)
  567. {
  568. if (r == -EDEADLK) {
  569. r = amdgpu_gpu_reset(adev);
  570. if (!r)
  571. r = -EAGAIN;
  572. }
  573. return r;
  574. }
  575. static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
  576. struct amdgpu_cs_parser *parser)
  577. {
  578. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  579. struct amdgpu_vm *vm = &fpriv->vm;
  580. int i, j;
  581. int r;
  582. for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) {
  583. struct amdgpu_cs_chunk *chunk;
  584. struct amdgpu_ib *ib;
  585. struct drm_amdgpu_cs_chunk_ib *chunk_ib;
  586. struct amdgpu_ring *ring;
  587. chunk = &parser->chunks[i];
  588. ib = &parser->job->ibs[j];
  589. chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
  590. if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
  591. continue;
  592. r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
  593. chunk_ib->ip_instance, chunk_ib->ring,
  594. &ring);
  595. if (r)
  596. return r;
  597. if (parser->job->ring && parser->job->ring != ring)
  598. return -EINVAL;
  599. parser->job->ring = ring;
  600. if (ring->funcs->parse_cs) {
  601. struct amdgpu_bo_va_mapping *m;
  602. struct amdgpu_bo *aobj = NULL;
  603. uint64_t offset;
  604. uint8_t *kptr;
  605. m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
  606. &aobj);
  607. if (!aobj) {
  608. DRM_ERROR("IB va_start is invalid\n");
  609. return -EINVAL;
  610. }
  611. if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
  612. (m->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
  613. DRM_ERROR("IB va_start+ib_bytes is invalid\n");
  614. return -EINVAL;
  615. }
  616. /* the IB should be reserved at this point */
  617. r = amdgpu_bo_kmap(aobj, (void **)&kptr);
  618. if (r) {
  619. return r;
  620. }
  621. offset = ((uint64_t)m->it.start) * AMDGPU_GPU_PAGE_SIZE;
  622. kptr += chunk_ib->va_start - offset;
  623. r = amdgpu_ib_get(adev, NULL, chunk_ib->ib_bytes, ib);
  624. if (r) {
  625. DRM_ERROR("Failed to get ib !\n");
  626. return r;
  627. }
  628. memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
  629. amdgpu_bo_kunmap(aobj);
  630. } else {
  631. r = amdgpu_ib_get(adev, vm, 0, ib);
  632. if (r) {
  633. DRM_ERROR("Failed to get ib !\n");
  634. return r;
  635. }
  636. ib->gpu_addr = chunk_ib->va_start;
  637. }
  638. ib->length_dw = chunk_ib->ib_bytes / 4;
  639. ib->flags = chunk_ib->flags;
  640. ib->ctx = parser->ctx;
  641. j++;
  642. }
  643. /* add GDS resources to first IB */
  644. if (parser->bo_list) {
  645. struct amdgpu_bo *gds = parser->bo_list->gds_obj;
  646. struct amdgpu_bo *gws = parser->bo_list->gws_obj;
  647. struct amdgpu_bo *oa = parser->bo_list->oa_obj;
  648. struct amdgpu_ib *ib = &parser->job->ibs[0];
  649. if (gds) {
  650. ib->gds_base = amdgpu_bo_gpu_offset(gds);
  651. ib->gds_size = amdgpu_bo_size(gds);
  652. }
  653. if (gws) {
  654. ib->gws_base = amdgpu_bo_gpu_offset(gws);
  655. ib->gws_size = amdgpu_bo_size(gws);
  656. }
  657. if (oa) {
  658. ib->oa_base = amdgpu_bo_gpu_offset(oa);
  659. ib->oa_size = amdgpu_bo_size(oa);
  660. }
  661. }
  662. /* wrap the last IB with user fence */
  663. if (parser->job->uf.bo) {
  664. struct amdgpu_ib *ib = &parser->job->ibs[parser->job->num_ibs - 1];
  665. /* UVD & VCE fw doesn't support user fences */
  666. if (parser->job->ring->type == AMDGPU_RING_TYPE_UVD ||
  667. parser->job->ring->type == AMDGPU_RING_TYPE_VCE)
  668. return -EINVAL;
  669. ib->user = &parser->job->uf;
  670. }
  671. return 0;
  672. }
  673. static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
  674. struct amdgpu_cs_parser *p)
  675. {
  676. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  677. int i, j, r;
  678. for (i = 0; i < p->nchunks; ++i) {
  679. struct drm_amdgpu_cs_chunk_dep *deps;
  680. struct amdgpu_cs_chunk *chunk;
  681. unsigned num_deps;
  682. chunk = &p->chunks[i];
  683. if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES)
  684. continue;
  685. deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
  686. num_deps = chunk->length_dw * 4 /
  687. sizeof(struct drm_amdgpu_cs_chunk_dep);
  688. for (j = 0; j < num_deps; ++j) {
  689. struct amdgpu_ring *ring;
  690. struct amdgpu_ctx *ctx;
  691. struct fence *fence;
  692. r = amdgpu_cs_get_ring(adev, deps[j].ip_type,
  693. deps[j].ip_instance,
  694. deps[j].ring, &ring);
  695. if (r)
  696. return r;
  697. ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id);
  698. if (ctx == NULL)
  699. return -EINVAL;
  700. fence = amdgpu_ctx_get_fence(ctx, ring,
  701. deps[j].handle);
  702. if (IS_ERR(fence)) {
  703. r = PTR_ERR(fence);
  704. amdgpu_ctx_put(ctx);
  705. return r;
  706. } else if (fence) {
  707. r = amdgpu_sync_fence(adev, &p->job->sync,
  708. fence);
  709. fence_put(fence);
  710. amdgpu_ctx_put(ctx);
  711. if (r)
  712. return r;
  713. }
  714. }
  715. }
  716. return 0;
  717. }
  718. static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
  719. union drm_amdgpu_cs *cs)
  720. {
  721. struct amdgpu_ring *ring = p->job->ring;
  722. struct amd_sched_fence *fence;
  723. struct amdgpu_job *job;
  724. job = p->job;
  725. p->job = NULL;
  726. job->base.sched = &ring->sched;
  727. job->base.s_entity = &p->ctx->rings[ring->idx].entity;
  728. job->owner = p->filp;
  729. fence = amd_sched_fence_create(job->base.s_entity, p->filp);
  730. if (!fence) {
  731. amdgpu_job_free(job);
  732. return -ENOMEM;
  733. }
  734. job->base.s_fence = fence;
  735. p->fence = fence_get(&fence->base);
  736. cs->out.handle = amdgpu_ctx_add_fence(p->ctx, ring,
  737. &fence->base);
  738. job->ibs[job->num_ibs - 1].sequence = cs->out.handle;
  739. trace_amdgpu_cs_ioctl(job);
  740. amd_sched_entity_push_job(&job->base);
  741. return 0;
  742. }
  743. int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  744. {
  745. struct amdgpu_device *adev = dev->dev_private;
  746. union drm_amdgpu_cs *cs = data;
  747. struct amdgpu_cs_parser parser = {};
  748. bool reserved_buffers = false;
  749. int i, r;
  750. if (!adev->accel_working)
  751. return -EBUSY;
  752. parser.adev = adev;
  753. parser.filp = filp;
  754. r = amdgpu_cs_parser_init(&parser, data);
  755. if (r) {
  756. DRM_ERROR("Failed to initialize parser !\n");
  757. amdgpu_cs_parser_fini(&parser, r, false);
  758. r = amdgpu_cs_handle_lockup(adev, r);
  759. return r;
  760. }
  761. r = amdgpu_cs_parser_bos(&parser, data);
  762. if (r == -ENOMEM)
  763. DRM_ERROR("Not enough memory for command submission!\n");
  764. else if (r && r != -ERESTARTSYS)
  765. DRM_ERROR("Failed to process the buffer list %d!\n", r);
  766. else if (!r) {
  767. reserved_buffers = true;
  768. r = amdgpu_cs_ib_fill(adev, &parser);
  769. }
  770. if (!r) {
  771. r = amdgpu_cs_dependencies(adev, &parser);
  772. if (r)
  773. DRM_ERROR("Failed in the dependencies handling %d!\n", r);
  774. }
  775. if (r)
  776. goto out;
  777. for (i = 0; i < parser.job->num_ibs; i++)
  778. trace_amdgpu_cs(&parser, i);
  779. r = amdgpu_cs_ib_vm_chunk(adev, &parser);
  780. if (r)
  781. goto out;
  782. r = amdgpu_cs_submit(&parser, cs);
  783. out:
  784. amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
  785. r = amdgpu_cs_handle_lockup(adev, r);
  786. return r;
  787. }
  788. /**
  789. * amdgpu_cs_wait_ioctl - wait for a command submission to finish
  790. *
  791. * @dev: drm device
  792. * @data: data from userspace
  793. * @filp: file private
  794. *
  795. * Wait for the command submission identified by handle to finish.
  796. */
  797. int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
  798. struct drm_file *filp)
  799. {
  800. union drm_amdgpu_wait_cs *wait = data;
  801. struct amdgpu_device *adev = dev->dev_private;
  802. unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
  803. struct amdgpu_ring *ring = NULL;
  804. struct amdgpu_ctx *ctx;
  805. struct fence *fence;
  806. long r;
  807. r = amdgpu_cs_get_ring(adev, wait->in.ip_type, wait->in.ip_instance,
  808. wait->in.ring, &ring);
  809. if (r)
  810. return r;
  811. ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
  812. if (ctx == NULL)
  813. return -EINVAL;
  814. fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
  815. if (IS_ERR(fence))
  816. r = PTR_ERR(fence);
  817. else if (fence) {
  818. r = fence_wait_timeout(fence, true, timeout);
  819. fence_put(fence);
  820. } else
  821. r = 1;
  822. amdgpu_ctx_put(ctx);
  823. if (r < 0)
  824. return r;
  825. memset(wait, 0, sizeof(*wait));
  826. wait->out.status = (r == 0);
  827. return 0;
  828. }
  829. /**
  830. * amdgpu_cs_find_bo_va - find bo_va for VM address
  831. *
  832. * @parser: command submission parser context
  833. * @addr: VM address
  834. * @bo: resulting BO of the mapping found
  835. *
  836. * Search the buffer objects in the command submission context for a certain
  837. * virtual memory address. Returns allocation structure when found, NULL
  838. * otherwise.
  839. */
  840. struct amdgpu_bo_va_mapping *
  841. amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
  842. uint64_t addr, struct amdgpu_bo **bo)
  843. {
  844. struct amdgpu_bo_va_mapping *mapping;
  845. unsigned i;
  846. if (!parser->bo_list)
  847. return NULL;
  848. addr /= AMDGPU_GPU_PAGE_SIZE;
  849. for (i = 0; i < parser->bo_list->num_entries; i++) {
  850. struct amdgpu_bo_list_entry *lobj;
  851. lobj = &parser->bo_list->array[i];
  852. if (!lobj->bo_va)
  853. continue;
  854. list_for_each_entry(mapping, &lobj->bo_va->valids, list) {
  855. if (mapping->it.start > addr ||
  856. addr > mapping->it.last)
  857. continue;
  858. *bo = lobj->bo_va->bo;
  859. return mapping;
  860. }
  861. list_for_each_entry(mapping, &lobj->bo_va->invalids, list) {
  862. if (mapping->it.start > addr ||
  863. addr > mapping->it.last)
  864. continue;
  865. *bo = lobj->bo_va->bo;
  866. return mapping;
  867. }
  868. }
  869. return NULL;
  870. }