amdgpu_cs.c 25 KB

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