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