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