amdgpu_cs.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404
  1. /*
  2. * Copyright 2008 Jerome Glisse.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors:
  25. * Jerome Glisse <glisse@freedesktop.org>
  26. */
  27. #include <linux/pagemap.h>
  28. #include <drm/drmP.h>
  29. #include <drm/amdgpu_drm.h>
  30. #include "amdgpu.h"
  31. #include "amdgpu_trace.h"
  32. int amdgpu_cs_get_ring(struct amdgpu_device *adev, u32 ip_type,
  33. u32 ip_instance, u32 ring,
  34. struct amdgpu_ring **out_ring)
  35. {
  36. /* Right now all IPs have only one instance - multiple rings. */
  37. if (ip_instance != 0) {
  38. DRM_ERROR("invalid ip instance: %d\n", ip_instance);
  39. return -EINVAL;
  40. }
  41. switch (ip_type) {
  42. default:
  43. DRM_ERROR("unknown ip type: %d\n", ip_type);
  44. return -EINVAL;
  45. case AMDGPU_HW_IP_GFX:
  46. if (ring < adev->gfx.num_gfx_rings) {
  47. *out_ring = &adev->gfx.gfx_ring[ring];
  48. } else {
  49. DRM_ERROR("only %d gfx rings are supported now\n",
  50. adev->gfx.num_gfx_rings);
  51. return -EINVAL;
  52. }
  53. break;
  54. case AMDGPU_HW_IP_COMPUTE:
  55. if (ring < adev->gfx.num_compute_rings) {
  56. *out_ring = &adev->gfx.compute_ring[ring];
  57. } else {
  58. DRM_ERROR("only %d compute rings are supported now\n",
  59. adev->gfx.num_compute_rings);
  60. return -EINVAL;
  61. }
  62. break;
  63. case AMDGPU_HW_IP_DMA:
  64. if (ring < adev->sdma.num_instances) {
  65. *out_ring = &adev->sdma.instance[ring].ring;
  66. } else {
  67. DRM_ERROR("only %d SDMA rings are supported\n",
  68. adev->sdma.num_instances);
  69. return -EINVAL;
  70. }
  71. break;
  72. case AMDGPU_HW_IP_UVD:
  73. *out_ring = &adev->uvd.ring;
  74. break;
  75. case AMDGPU_HW_IP_VCE:
  76. if (ring < adev->vce.num_rings){
  77. *out_ring = &adev->vce.ring[ring];
  78. } else {
  79. DRM_ERROR("only %d VCE rings are supported\n", adev->vce.num_rings);
  80. return -EINVAL;
  81. }
  82. break;
  83. }
  84. return 0;
  85. }
  86. static int amdgpu_cs_user_fence_chunk(struct amdgpu_cs_parser *p,
  87. struct drm_amdgpu_cs_chunk_fence *data,
  88. uint32_t *offset)
  89. {
  90. struct drm_gem_object *gobj;
  91. unsigned long size;
  92. gobj = drm_gem_object_lookup(p->filp, data->handle);
  93. if (gobj == NULL)
  94. return -EINVAL;
  95. p->uf_entry.robj = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
  96. p->uf_entry.priority = 0;
  97. p->uf_entry.tv.bo = &p->uf_entry.robj->tbo;
  98. p->uf_entry.tv.shared = true;
  99. p->uf_entry.user_pages = NULL;
  100. size = amdgpu_bo_size(p->uf_entry.robj);
  101. if (size != PAGE_SIZE || (data->offset + 8) > size)
  102. return -EINVAL;
  103. *offset = data->offset;
  104. drm_gem_object_unreference_unlocked(gobj);
  105. if (amdgpu_ttm_tt_get_usermm(p->uf_entry.robj->tbo.ttm)) {
  106. amdgpu_bo_unref(&p->uf_entry.robj);
  107. return -EINVAL;
  108. }
  109. return 0;
  110. }
  111. int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, void *data)
  112. {
  113. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  114. struct amdgpu_vm *vm = &fpriv->vm;
  115. union drm_amdgpu_cs *cs = data;
  116. uint64_t *chunk_array_user;
  117. uint64_t *chunk_array;
  118. unsigned size, num_ibs = 0;
  119. uint32_t uf_offset = 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, p->chunks[i].kdata,
  183. &uf_offset);
  184. if (ret)
  185. goto free_partial_kdata;
  186. break;
  187. case AMDGPU_CHUNK_ID_DEPENDENCIES:
  188. break;
  189. default:
  190. ret = -EINVAL;
  191. goto free_partial_kdata;
  192. }
  193. }
  194. ret = amdgpu_job_alloc(p->adev, num_ibs, &p->job, vm);
  195. if (ret)
  196. goto free_all_kdata;
  197. if (p->uf_entry.robj)
  198. p->job->uf_addr = uf_offset;
  199. kfree(chunk_array);
  200. return 0;
  201. free_all_kdata:
  202. i = p->nchunks - 1;
  203. free_partial_kdata:
  204. for (; i >= 0; i--)
  205. drm_free_large(p->chunks[i].kdata);
  206. kfree(p->chunks);
  207. put_ctx:
  208. amdgpu_ctx_put(p->ctx);
  209. free_chunk:
  210. kfree(chunk_array);
  211. return ret;
  212. }
  213. /* Convert microseconds to bytes. */
  214. static u64 us_to_bytes(struct amdgpu_device *adev, s64 us)
  215. {
  216. if (us <= 0 || !adev->mm_stats.log2_max_MBps)
  217. return 0;
  218. /* Since accum_us is incremented by a million per second, just
  219. * multiply it by the number of MB/s to get the number of bytes.
  220. */
  221. return us << adev->mm_stats.log2_max_MBps;
  222. }
  223. static s64 bytes_to_us(struct amdgpu_device *adev, u64 bytes)
  224. {
  225. if (!adev->mm_stats.log2_max_MBps)
  226. return 0;
  227. return bytes >> adev->mm_stats.log2_max_MBps;
  228. }
  229. /* Returns how many bytes TTM can move right now. If no bytes can be moved,
  230. * it returns 0. If it returns non-zero, it's OK to move at least one buffer,
  231. * which means it can go over the threshold once. If that happens, the driver
  232. * will be in debt and no other buffer migrations can be done until that debt
  233. * is repaid.
  234. *
  235. * This approach allows moving a buffer of any size (it's important to allow
  236. * that).
  237. *
  238. * The currency is simply time in microseconds and it increases as the clock
  239. * ticks. The accumulated microseconds (us) are converted to bytes and
  240. * returned.
  241. */
  242. static u64 amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev)
  243. {
  244. s64 time_us, increment_us;
  245. u64 max_bytes;
  246. u64 free_vram, total_vram, used_vram;
  247. /* Allow a maximum of 200 accumulated ms. This is basically per-IB
  248. * throttling.
  249. *
  250. * It means that in order to get full max MBps, at least 5 IBs per
  251. * second must be submitted and not more than 200ms apart from each
  252. * other.
  253. */
  254. const s64 us_upper_bound = 200000;
  255. if (!adev->mm_stats.log2_max_MBps)
  256. return 0;
  257. total_vram = adev->mc.real_vram_size - adev->vram_pin_size;
  258. used_vram = atomic64_read(&adev->vram_usage);
  259. free_vram = used_vram >= total_vram ? 0 : total_vram - used_vram;
  260. spin_lock(&adev->mm_stats.lock);
  261. /* Increase the amount of accumulated us. */
  262. time_us = ktime_to_us(ktime_get());
  263. increment_us = time_us - adev->mm_stats.last_update_us;
  264. adev->mm_stats.last_update_us = time_us;
  265. adev->mm_stats.accum_us = min(adev->mm_stats.accum_us + increment_us,
  266. us_upper_bound);
  267. /* This prevents the short period of low performance when the VRAM
  268. * usage is low and the driver is in debt or doesn't have enough
  269. * accumulated us to fill VRAM quickly.
  270. *
  271. * The situation can occur in these cases:
  272. * - a lot of VRAM is freed by userspace
  273. * - the presence of a big buffer causes a lot of evictions
  274. * (solution: split buffers into smaller ones)
  275. *
  276. * If 128 MB or 1/8th of VRAM is free, start filling it now by setting
  277. * accum_us to a positive number.
  278. */
  279. if (free_vram >= 128 * 1024 * 1024 || free_vram >= total_vram / 8) {
  280. s64 min_us;
  281. /* Be more aggresive on dGPUs. Try to fill a portion of free
  282. * VRAM now.
  283. */
  284. if (!(adev->flags & AMD_IS_APU))
  285. min_us = bytes_to_us(adev, free_vram / 4);
  286. else
  287. min_us = 0; /* Reset accum_us on APUs. */
  288. adev->mm_stats.accum_us = max(min_us, adev->mm_stats.accum_us);
  289. }
  290. /* This returns 0 if the driver is in debt to disallow (optional)
  291. * buffer moves.
  292. */
  293. max_bytes = us_to_bytes(adev, adev->mm_stats.accum_us);
  294. spin_unlock(&adev->mm_stats.lock);
  295. return max_bytes;
  296. }
  297. /* Report how many bytes have really been moved for the last command
  298. * submission. This can result in a debt that can stop buffer migrations
  299. * temporarily.
  300. */
  301. void amdgpu_cs_report_moved_bytes(struct amdgpu_device *adev, u64 num_bytes)
  302. {
  303. spin_lock(&adev->mm_stats.lock);
  304. adev->mm_stats.accum_us -= bytes_to_us(adev, num_bytes);
  305. spin_unlock(&adev->mm_stats.lock);
  306. }
  307. static int amdgpu_cs_bo_validate(struct amdgpu_cs_parser *p,
  308. struct amdgpu_bo *bo)
  309. {
  310. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  311. u64 initial_bytes_moved;
  312. uint32_t domain;
  313. int r;
  314. if (bo->pin_count)
  315. return 0;
  316. /* Don't move this buffer if we have depleted our allowance
  317. * to move it. Don't move anything if the threshold is zero.
  318. */
  319. if (p->bytes_moved < p->bytes_moved_threshold)
  320. domain = bo->prefered_domains;
  321. else
  322. domain = bo->allowed_domains;
  323. retry:
  324. amdgpu_ttm_placement_from_domain(bo, domain);
  325. initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
  326. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  327. p->bytes_moved += atomic64_read(&adev->num_bytes_moved) -
  328. initial_bytes_moved;
  329. if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) {
  330. domain = bo->allowed_domains;
  331. goto retry;
  332. }
  333. return r;
  334. }
  335. /* Last resort, try to evict something from the current working set */
  336. static bool amdgpu_cs_try_evict(struct amdgpu_cs_parser *p,
  337. struct amdgpu_bo *validated)
  338. {
  339. uint32_t domain = validated->allowed_domains;
  340. int r;
  341. if (!p->evictable)
  342. return false;
  343. for (;&p->evictable->tv.head != &p->validated;
  344. p->evictable = list_prev_entry(p->evictable, tv.head)) {
  345. struct amdgpu_bo_list_entry *candidate = p->evictable;
  346. struct amdgpu_bo *bo = candidate->robj;
  347. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  348. u64 initial_bytes_moved;
  349. uint32_t other;
  350. /* If we reached our current BO we can forget it */
  351. if (candidate->robj == validated)
  352. break;
  353. other = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
  354. /* Check if this BO is in one of the domains we need space for */
  355. if (!(other & domain))
  356. continue;
  357. /* Check if we can move this BO somewhere else */
  358. other = bo->allowed_domains & ~domain;
  359. if (!other)
  360. continue;
  361. /* Good we can try to move this BO somewhere else */
  362. amdgpu_ttm_placement_from_domain(bo, other);
  363. initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
  364. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  365. p->bytes_moved += atomic64_read(&adev->num_bytes_moved) -
  366. initial_bytes_moved;
  367. if (unlikely(r))
  368. break;
  369. p->evictable = list_prev_entry(p->evictable, tv.head);
  370. list_move(&candidate->tv.head, &p->validated);
  371. return true;
  372. }
  373. return false;
  374. }
  375. static int amdgpu_cs_validate(void *param, struct amdgpu_bo *bo)
  376. {
  377. struct amdgpu_cs_parser *p = param;
  378. int r;
  379. do {
  380. r = amdgpu_cs_bo_validate(p, bo);
  381. } while (r == -ENOMEM && amdgpu_cs_try_evict(p, bo));
  382. if (r)
  383. return r;
  384. if (bo->shadow)
  385. r = amdgpu_cs_bo_validate(p, bo->shadow);
  386. return r;
  387. }
  388. static int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p,
  389. struct list_head *validated)
  390. {
  391. struct amdgpu_bo_list_entry *lobj;
  392. int r;
  393. list_for_each_entry(lobj, validated, tv.head) {
  394. struct amdgpu_bo *bo = lobj->robj;
  395. bool binding_userptr = false;
  396. struct mm_struct *usermm;
  397. usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
  398. if (usermm && usermm != current->mm)
  399. return -EPERM;
  400. /* Check if we have user pages and nobody bound the BO already */
  401. if (lobj->user_pages && bo->tbo.ttm->state != tt_bound) {
  402. size_t size = sizeof(struct page *);
  403. size *= bo->tbo.ttm->num_pages;
  404. memcpy(bo->tbo.ttm->pages, lobj->user_pages, size);
  405. binding_userptr = true;
  406. }
  407. if (p->evictable == lobj)
  408. p->evictable = NULL;
  409. r = amdgpu_cs_validate(p, bo);
  410. if (r)
  411. return r;
  412. if (binding_userptr) {
  413. drm_free_large(lobj->user_pages);
  414. lobj->user_pages = NULL;
  415. }
  416. }
  417. return 0;
  418. }
  419. static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
  420. union drm_amdgpu_cs *cs)
  421. {
  422. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  423. struct amdgpu_bo_list_entry *e;
  424. struct list_head duplicates;
  425. bool need_mmap_lock = false;
  426. unsigned i, tries = 10;
  427. int r;
  428. INIT_LIST_HEAD(&p->validated);
  429. p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
  430. if (p->bo_list) {
  431. need_mmap_lock = p->bo_list->first_userptr !=
  432. p->bo_list->num_entries;
  433. amdgpu_bo_list_get_list(p->bo_list, &p->validated);
  434. }
  435. INIT_LIST_HEAD(&duplicates);
  436. amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
  437. if (p->uf_entry.robj)
  438. list_add(&p->uf_entry.tv.head, &p->validated);
  439. if (need_mmap_lock)
  440. down_read(&current->mm->mmap_sem);
  441. while (1) {
  442. struct list_head need_pages;
  443. unsigned i;
  444. r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
  445. &duplicates);
  446. if (unlikely(r != 0)) {
  447. if (r != -ERESTARTSYS)
  448. DRM_ERROR("ttm_eu_reserve_buffers failed.\n");
  449. goto error_free_pages;
  450. }
  451. /* Without a BO list we don't have userptr BOs */
  452. if (!p->bo_list)
  453. break;
  454. INIT_LIST_HEAD(&need_pages);
  455. for (i = p->bo_list->first_userptr;
  456. i < p->bo_list->num_entries; ++i) {
  457. e = &p->bo_list->array[i];
  458. if (amdgpu_ttm_tt_userptr_invalidated(e->robj->tbo.ttm,
  459. &e->user_invalidated) && e->user_pages) {
  460. /* We acquired a page array, but somebody
  461. * invalidated it. Free it an try again
  462. */
  463. release_pages(e->user_pages,
  464. e->robj->tbo.ttm->num_pages,
  465. false);
  466. drm_free_large(e->user_pages);
  467. e->user_pages = NULL;
  468. }
  469. if (e->robj->tbo.ttm->state != tt_bound &&
  470. !e->user_pages) {
  471. list_del(&e->tv.head);
  472. list_add(&e->tv.head, &need_pages);
  473. amdgpu_bo_unreserve(e->robj);
  474. }
  475. }
  476. if (list_empty(&need_pages))
  477. break;
  478. /* Unreserve everything again. */
  479. ttm_eu_backoff_reservation(&p->ticket, &p->validated);
  480. /* We tried too many times, just abort */
  481. if (!--tries) {
  482. r = -EDEADLK;
  483. DRM_ERROR("deadlock in %s\n", __func__);
  484. goto error_free_pages;
  485. }
  486. /* Fill the page arrays for all useptrs. */
  487. list_for_each_entry(e, &need_pages, tv.head) {
  488. struct ttm_tt *ttm = e->robj->tbo.ttm;
  489. e->user_pages = drm_calloc_large(ttm->num_pages,
  490. sizeof(struct page*));
  491. if (!e->user_pages) {
  492. r = -ENOMEM;
  493. DRM_ERROR("calloc failure in %s\n", __func__);
  494. goto error_free_pages;
  495. }
  496. r = amdgpu_ttm_tt_get_user_pages(ttm, e->user_pages);
  497. if (r) {
  498. DRM_ERROR("amdgpu_ttm_tt_get_user_pages failed.\n");
  499. drm_free_large(e->user_pages);
  500. e->user_pages = NULL;
  501. goto error_free_pages;
  502. }
  503. }
  504. /* And try again. */
  505. list_splice(&need_pages, &p->validated);
  506. }
  507. p->bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(p->adev);
  508. p->bytes_moved = 0;
  509. p->evictable = list_last_entry(&p->validated,
  510. struct amdgpu_bo_list_entry,
  511. tv.head);
  512. r = amdgpu_vm_validate_pt_bos(p->adev, &fpriv->vm,
  513. amdgpu_cs_validate, p);
  514. if (r) {
  515. DRM_ERROR("amdgpu_vm_validate_pt_bos() failed.\n");
  516. goto error_validate;
  517. }
  518. r = amdgpu_cs_list_validate(p, &duplicates);
  519. if (r) {
  520. DRM_ERROR("amdgpu_cs_list_validate(duplicates) failed.\n");
  521. goto error_validate;
  522. }
  523. r = amdgpu_cs_list_validate(p, &p->validated);
  524. if (r) {
  525. DRM_ERROR("amdgpu_cs_list_validate(validated) failed.\n");
  526. goto error_validate;
  527. }
  528. amdgpu_cs_report_moved_bytes(p->adev, p->bytes_moved);
  529. fpriv->vm.last_eviction_counter =
  530. atomic64_read(&p->adev->num_evictions);
  531. if (p->bo_list) {
  532. struct amdgpu_bo *gds = p->bo_list->gds_obj;
  533. struct amdgpu_bo *gws = p->bo_list->gws_obj;
  534. struct amdgpu_bo *oa = p->bo_list->oa_obj;
  535. struct amdgpu_vm *vm = &fpriv->vm;
  536. unsigned i;
  537. for (i = 0; i < p->bo_list->num_entries; i++) {
  538. struct amdgpu_bo *bo = p->bo_list->array[i].robj;
  539. p->bo_list->array[i].bo_va = amdgpu_vm_bo_find(vm, bo);
  540. }
  541. if (gds) {
  542. p->job->gds_base = amdgpu_bo_gpu_offset(gds);
  543. p->job->gds_size = amdgpu_bo_size(gds);
  544. }
  545. if (gws) {
  546. p->job->gws_base = amdgpu_bo_gpu_offset(gws);
  547. p->job->gws_size = amdgpu_bo_size(gws);
  548. }
  549. if (oa) {
  550. p->job->oa_base = amdgpu_bo_gpu_offset(oa);
  551. p->job->oa_size = amdgpu_bo_size(oa);
  552. }
  553. }
  554. if (!r && p->uf_entry.robj) {
  555. struct amdgpu_bo *uf = p->uf_entry.robj;
  556. r = amdgpu_ttm_bind(&uf->tbo, &uf->tbo.mem);
  557. p->job->uf_addr += amdgpu_bo_gpu_offset(uf);
  558. }
  559. error_validate:
  560. if (r) {
  561. amdgpu_vm_move_pt_bos_in_lru(p->adev, &fpriv->vm);
  562. ttm_eu_backoff_reservation(&p->ticket, &p->validated);
  563. }
  564. error_free_pages:
  565. if (need_mmap_lock)
  566. up_read(&current->mm->mmap_sem);
  567. if (p->bo_list) {
  568. for (i = p->bo_list->first_userptr;
  569. i < p->bo_list->num_entries; ++i) {
  570. e = &p->bo_list->array[i];
  571. if (!e->user_pages)
  572. continue;
  573. release_pages(e->user_pages,
  574. e->robj->tbo.ttm->num_pages,
  575. false);
  576. drm_free_large(e->user_pages);
  577. }
  578. }
  579. return r;
  580. }
  581. static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
  582. {
  583. struct amdgpu_bo_list_entry *e;
  584. int r;
  585. list_for_each_entry(e, &p->validated, tv.head) {
  586. struct reservation_object *resv = e->robj->tbo.resv;
  587. r = amdgpu_sync_resv(p->adev, &p->job->sync, resv, p->filp);
  588. if (r)
  589. return r;
  590. }
  591. return 0;
  592. }
  593. /**
  594. * cs_parser_fini() - clean parser states
  595. * @parser: parser structure holding parsing context.
  596. * @error: error number
  597. *
  598. * If error is set than unvalidate buffer, otherwise just free memory
  599. * used by parsing context.
  600. **/
  601. static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
  602. {
  603. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  604. unsigned i;
  605. if (!error) {
  606. amdgpu_vm_move_pt_bos_in_lru(parser->adev, &fpriv->vm);
  607. ttm_eu_fence_buffer_objects(&parser->ticket,
  608. &parser->validated,
  609. parser->fence);
  610. } else if (backoff) {
  611. ttm_eu_backoff_reservation(&parser->ticket,
  612. &parser->validated);
  613. }
  614. dma_fence_put(parser->fence);
  615. if (parser->ctx)
  616. amdgpu_ctx_put(parser->ctx);
  617. if (parser->bo_list)
  618. amdgpu_bo_list_put(parser->bo_list);
  619. for (i = 0; i < parser->nchunks; i++)
  620. drm_free_large(parser->chunks[i].kdata);
  621. kfree(parser->chunks);
  622. if (parser->job)
  623. amdgpu_job_free(parser->job);
  624. amdgpu_bo_unref(&parser->uf_entry.robj);
  625. }
  626. static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p,
  627. struct amdgpu_vm *vm)
  628. {
  629. struct amdgpu_device *adev = p->adev;
  630. struct amdgpu_bo_va *bo_va;
  631. struct amdgpu_bo *bo;
  632. int i, r;
  633. r = amdgpu_vm_update_page_directory(adev, vm);
  634. if (r)
  635. return r;
  636. r = amdgpu_sync_fence(adev, &p->job->sync, vm->page_directory_fence);
  637. if (r)
  638. return r;
  639. r = amdgpu_vm_clear_freed(adev, vm);
  640. if (r)
  641. return r;
  642. if (amdgpu_sriov_vf(adev)) {
  643. struct dma_fence *f;
  644. bo_va = vm->csa_bo_va;
  645. BUG_ON(!bo_va);
  646. r = amdgpu_vm_bo_update(adev, bo_va, false);
  647. if (r)
  648. return r;
  649. f = bo_va->last_pt_update;
  650. r = amdgpu_sync_fence(adev, &p->job->sync, f);
  651. if (r)
  652. return r;
  653. }
  654. if (p->bo_list) {
  655. for (i = 0; i < p->bo_list->num_entries; i++) {
  656. struct dma_fence *f;
  657. /* ignore duplicates */
  658. bo = p->bo_list->array[i].robj;
  659. if (!bo)
  660. continue;
  661. bo_va = p->bo_list->array[i].bo_va;
  662. if (bo_va == NULL)
  663. continue;
  664. r = amdgpu_vm_bo_update(adev, bo_va, false);
  665. if (r)
  666. return r;
  667. f = bo_va->last_pt_update;
  668. r = amdgpu_sync_fence(adev, &p->job->sync, f);
  669. if (r)
  670. return r;
  671. }
  672. }
  673. r = amdgpu_vm_clear_invalids(adev, vm, &p->job->sync);
  674. if (amdgpu_vm_debug && p->bo_list) {
  675. /* Invalidate all BOs to test for userspace bugs */
  676. for (i = 0; i < p->bo_list->num_entries; i++) {
  677. /* ignore duplicates */
  678. bo = p->bo_list->array[i].robj;
  679. if (!bo)
  680. continue;
  681. amdgpu_vm_bo_invalidate(adev, bo);
  682. }
  683. }
  684. return r;
  685. }
  686. static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
  687. struct amdgpu_cs_parser *p)
  688. {
  689. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  690. struct amdgpu_vm *vm = &fpriv->vm;
  691. struct amdgpu_ring *ring = p->job->ring;
  692. int i, r;
  693. /* Only for UVD/VCE VM emulation */
  694. if (ring->funcs->parse_cs) {
  695. for (i = 0; i < p->job->num_ibs; i++) {
  696. r = amdgpu_ring_parse_cs(ring, p, i);
  697. if (r)
  698. return r;
  699. }
  700. }
  701. if (p->job->vm) {
  702. p->job->vm_pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
  703. r = amdgpu_bo_vm_update_pte(p, vm);
  704. if (r)
  705. return r;
  706. }
  707. return amdgpu_cs_sync_rings(p);
  708. }
  709. static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
  710. struct amdgpu_cs_parser *parser)
  711. {
  712. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  713. struct amdgpu_vm *vm = &fpriv->vm;
  714. int i, j;
  715. int r;
  716. for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) {
  717. struct amdgpu_cs_chunk *chunk;
  718. struct amdgpu_ib *ib;
  719. struct drm_amdgpu_cs_chunk_ib *chunk_ib;
  720. struct amdgpu_ring *ring;
  721. chunk = &parser->chunks[i];
  722. ib = &parser->job->ibs[j];
  723. chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
  724. if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
  725. continue;
  726. r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
  727. chunk_ib->ip_instance, chunk_ib->ring,
  728. &ring);
  729. if (r)
  730. return r;
  731. if (ib->flags & AMDGPU_IB_FLAG_PREAMBLE) {
  732. parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT;
  733. if (!parser->ctx->preamble_presented) {
  734. parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT_FIRST;
  735. parser->ctx->preamble_presented = true;
  736. }
  737. }
  738. if (parser->job->ring && parser->job->ring != ring)
  739. return -EINVAL;
  740. parser->job->ring = ring;
  741. if (ring->funcs->parse_cs) {
  742. struct amdgpu_bo_va_mapping *m;
  743. struct amdgpu_bo *aobj = NULL;
  744. uint64_t offset;
  745. uint8_t *kptr;
  746. m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
  747. &aobj);
  748. if (!aobj) {
  749. DRM_ERROR("IB va_start is invalid\n");
  750. return -EINVAL;
  751. }
  752. if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
  753. (m->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
  754. DRM_ERROR("IB va_start+ib_bytes is invalid\n");
  755. return -EINVAL;
  756. }
  757. /* the IB should be reserved at this point */
  758. r = amdgpu_bo_kmap(aobj, (void **)&kptr);
  759. if (r) {
  760. return r;
  761. }
  762. offset = ((uint64_t)m->it.start) * AMDGPU_GPU_PAGE_SIZE;
  763. kptr += chunk_ib->va_start - offset;
  764. r = amdgpu_ib_get(adev, vm, chunk_ib->ib_bytes, ib);
  765. if (r) {
  766. DRM_ERROR("Failed to get ib !\n");
  767. return r;
  768. }
  769. memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
  770. amdgpu_bo_kunmap(aobj);
  771. } else {
  772. r = amdgpu_ib_get(adev, vm, 0, ib);
  773. if (r) {
  774. DRM_ERROR("Failed to get ib !\n");
  775. return r;
  776. }
  777. }
  778. ib->gpu_addr = chunk_ib->va_start;
  779. ib->length_dw = chunk_ib->ib_bytes / 4;
  780. ib->flags = chunk_ib->flags;
  781. j++;
  782. }
  783. /* UVD & VCE fw doesn't support user fences */
  784. if (parser->job->uf_addr && (
  785. parser->job->ring->funcs->type == AMDGPU_RING_TYPE_UVD ||
  786. parser->job->ring->funcs->type == AMDGPU_RING_TYPE_VCE))
  787. return -EINVAL;
  788. return 0;
  789. }
  790. static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
  791. struct amdgpu_cs_parser *p)
  792. {
  793. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  794. int i, j, r;
  795. for (i = 0; i < p->nchunks; ++i) {
  796. struct drm_amdgpu_cs_chunk_dep *deps;
  797. struct amdgpu_cs_chunk *chunk;
  798. unsigned num_deps;
  799. chunk = &p->chunks[i];
  800. if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES)
  801. continue;
  802. deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
  803. num_deps = chunk->length_dw * 4 /
  804. sizeof(struct drm_amdgpu_cs_chunk_dep);
  805. for (j = 0; j < num_deps; ++j) {
  806. struct amdgpu_ring *ring;
  807. struct amdgpu_ctx *ctx;
  808. struct dma_fence *fence;
  809. r = amdgpu_cs_get_ring(adev, deps[j].ip_type,
  810. deps[j].ip_instance,
  811. deps[j].ring, &ring);
  812. if (r)
  813. return r;
  814. ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id);
  815. if (ctx == NULL)
  816. return -EINVAL;
  817. fence = amdgpu_ctx_get_fence(ctx, ring,
  818. deps[j].handle);
  819. if (IS_ERR(fence)) {
  820. r = PTR_ERR(fence);
  821. amdgpu_ctx_put(ctx);
  822. return r;
  823. } else if (fence) {
  824. r = amdgpu_sync_fence(adev, &p->job->sync,
  825. fence);
  826. dma_fence_put(fence);
  827. amdgpu_ctx_put(ctx);
  828. if (r)
  829. return r;
  830. }
  831. }
  832. }
  833. return 0;
  834. }
  835. static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
  836. union drm_amdgpu_cs *cs)
  837. {
  838. struct amdgpu_ring *ring = p->job->ring;
  839. struct amd_sched_entity *entity = &p->ctx->rings[ring->idx].entity;
  840. struct amdgpu_job *job;
  841. int r;
  842. job = p->job;
  843. p->job = NULL;
  844. r = amd_sched_job_init(&job->base, &ring->sched, entity, p->filp);
  845. if (r) {
  846. amdgpu_job_free(job);
  847. return r;
  848. }
  849. job->owner = p->filp;
  850. job->fence_ctx = entity->fence_context;
  851. p->fence = dma_fence_get(&job->base.s_fence->finished);
  852. cs->out.handle = amdgpu_ctx_add_fence(p->ctx, ring, p->fence);
  853. job->uf_sequence = cs->out.handle;
  854. amdgpu_job_free_resources(job);
  855. trace_amdgpu_cs_ioctl(job);
  856. amd_sched_entity_push_job(&job->base);
  857. return 0;
  858. }
  859. int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  860. {
  861. struct amdgpu_device *adev = dev->dev_private;
  862. union drm_amdgpu_cs *cs = data;
  863. struct amdgpu_cs_parser parser = {};
  864. bool reserved_buffers = false;
  865. int i, r;
  866. if (!adev->accel_working)
  867. return -EBUSY;
  868. parser.adev = adev;
  869. parser.filp = filp;
  870. r = amdgpu_cs_parser_init(&parser, data);
  871. if (r) {
  872. DRM_ERROR("Failed to initialize parser !\n");
  873. goto out;
  874. }
  875. r = amdgpu_cs_parser_bos(&parser, data);
  876. if (r) {
  877. if (r == -ENOMEM)
  878. DRM_ERROR("Not enough memory for command submission!\n");
  879. else if (r != -ERESTARTSYS)
  880. DRM_ERROR("Failed to process the buffer list %d!\n", r);
  881. goto out;
  882. }
  883. reserved_buffers = true;
  884. r = amdgpu_cs_ib_fill(adev, &parser);
  885. if (r)
  886. goto out;
  887. r = amdgpu_cs_dependencies(adev, &parser);
  888. if (r) {
  889. DRM_ERROR("Failed in the dependencies handling %d!\n", r);
  890. goto out;
  891. }
  892. for (i = 0; i < parser.job->num_ibs; i++)
  893. trace_amdgpu_cs(&parser, i);
  894. r = amdgpu_cs_ib_vm_chunk(adev, &parser);
  895. if (r)
  896. goto out;
  897. r = amdgpu_cs_submit(&parser, cs);
  898. out:
  899. amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
  900. return r;
  901. }
  902. /**
  903. * amdgpu_cs_wait_ioctl - wait for a command submission to finish
  904. *
  905. * @dev: drm device
  906. * @data: data from userspace
  907. * @filp: file private
  908. *
  909. * Wait for the command submission identified by handle to finish.
  910. */
  911. int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
  912. struct drm_file *filp)
  913. {
  914. union drm_amdgpu_wait_cs *wait = data;
  915. struct amdgpu_device *adev = dev->dev_private;
  916. unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
  917. struct amdgpu_ring *ring = NULL;
  918. struct amdgpu_ctx *ctx;
  919. struct dma_fence *fence;
  920. long r;
  921. r = amdgpu_cs_get_ring(adev, wait->in.ip_type, wait->in.ip_instance,
  922. wait->in.ring, &ring);
  923. if (r)
  924. return r;
  925. ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
  926. if (ctx == NULL)
  927. return -EINVAL;
  928. fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
  929. if (IS_ERR(fence))
  930. r = PTR_ERR(fence);
  931. else if (fence) {
  932. r = dma_fence_wait_timeout(fence, true, timeout);
  933. dma_fence_put(fence);
  934. } else
  935. r = 1;
  936. amdgpu_ctx_put(ctx);
  937. if (r < 0)
  938. return r;
  939. memset(wait, 0, sizeof(*wait));
  940. wait->out.status = (r == 0);
  941. return 0;
  942. }
  943. /**
  944. * amdgpu_cs_get_fence - helper to get fence from drm_amdgpu_fence
  945. *
  946. * @adev: amdgpu device
  947. * @filp: file private
  948. * @user: drm_amdgpu_fence copied from user space
  949. */
  950. static struct dma_fence *amdgpu_cs_get_fence(struct amdgpu_device *adev,
  951. struct drm_file *filp,
  952. struct drm_amdgpu_fence *user)
  953. {
  954. struct amdgpu_ring *ring;
  955. struct amdgpu_ctx *ctx;
  956. struct dma_fence *fence;
  957. int r;
  958. r = amdgpu_cs_get_ring(adev, user->ip_type, user->ip_instance,
  959. user->ring, &ring);
  960. if (r)
  961. return ERR_PTR(r);
  962. ctx = amdgpu_ctx_get(filp->driver_priv, user->ctx_id);
  963. if (ctx == NULL)
  964. return ERR_PTR(-EINVAL);
  965. fence = amdgpu_ctx_get_fence(ctx, ring, user->seq_no);
  966. amdgpu_ctx_put(ctx);
  967. return fence;
  968. }
  969. /**
  970. * amdgpu_cs_wait_all_fence - wait on all fences to signal
  971. *
  972. * @adev: amdgpu device
  973. * @filp: file private
  974. * @wait: wait parameters
  975. * @fences: array of drm_amdgpu_fence
  976. */
  977. static int amdgpu_cs_wait_all_fences(struct amdgpu_device *adev,
  978. struct drm_file *filp,
  979. union drm_amdgpu_wait_fences *wait,
  980. struct drm_amdgpu_fence *fences)
  981. {
  982. uint32_t fence_count = wait->in.fence_count;
  983. unsigned int i;
  984. long r = 1;
  985. for (i = 0; i < fence_count; i++) {
  986. struct dma_fence *fence;
  987. unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
  988. fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
  989. if (IS_ERR(fence))
  990. return PTR_ERR(fence);
  991. else if (!fence)
  992. continue;
  993. r = dma_fence_wait_timeout(fence, true, timeout);
  994. if (r < 0)
  995. return r;
  996. if (r == 0)
  997. break;
  998. }
  999. memset(wait, 0, sizeof(*wait));
  1000. wait->out.status = (r > 0);
  1001. return 0;
  1002. }
  1003. /**
  1004. * amdgpu_cs_wait_any_fence - wait on any fence to signal
  1005. *
  1006. * @adev: amdgpu device
  1007. * @filp: file private
  1008. * @wait: wait parameters
  1009. * @fences: array of drm_amdgpu_fence
  1010. */
  1011. static int amdgpu_cs_wait_any_fence(struct amdgpu_device *adev,
  1012. struct drm_file *filp,
  1013. union drm_amdgpu_wait_fences *wait,
  1014. struct drm_amdgpu_fence *fences)
  1015. {
  1016. unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
  1017. uint32_t fence_count = wait->in.fence_count;
  1018. uint32_t first = ~0;
  1019. struct dma_fence **array;
  1020. unsigned int i;
  1021. long r;
  1022. /* Prepare the fence array */
  1023. array = kcalloc(fence_count, sizeof(struct dma_fence *), GFP_KERNEL);
  1024. if (array == NULL)
  1025. return -ENOMEM;
  1026. for (i = 0; i < fence_count; i++) {
  1027. struct dma_fence *fence;
  1028. fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
  1029. if (IS_ERR(fence)) {
  1030. r = PTR_ERR(fence);
  1031. goto err_free_fence_array;
  1032. } else if (fence) {
  1033. array[i] = fence;
  1034. } else { /* NULL, the fence has been already signaled */
  1035. r = 1;
  1036. goto out;
  1037. }
  1038. }
  1039. r = dma_fence_wait_any_timeout(array, fence_count, true, timeout,
  1040. &first);
  1041. if (r < 0)
  1042. goto err_free_fence_array;
  1043. out:
  1044. memset(wait, 0, sizeof(*wait));
  1045. wait->out.status = (r > 0);
  1046. wait->out.first_signaled = first;
  1047. /* set return value 0 to indicate success */
  1048. r = 0;
  1049. err_free_fence_array:
  1050. for (i = 0; i < fence_count; i++)
  1051. dma_fence_put(array[i]);
  1052. kfree(array);
  1053. return r;
  1054. }
  1055. /**
  1056. * amdgpu_cs_wait_fences_ioctl - wait for multiple command submissions to finish
  1057. *
  1058. * @dev: drm device
  1059. * @data: data from userspace
  1060. * @filp: file private
  1061. */
  1062. int amdgpu_cs_wait_fences_ioctl(struct drm_device *dev, void *data,
  1063. struct drm_file *filp)
  1064. {
  1065. struct amdgpu_device *adev = dev->dev_private;
  1066. union drm_amdgpu_wait_fences *wait = data;
  1067. uint32_t fence_count = wait->in.fence_count;
  1068. struct drm_amdgpu_fence *fences_user;
  1069. struct drm_amdgpu_fence *fences;
  1070. int r;
  1071. /* Get the fences from userspace */
  1072. fences = kmalloc_array(fence_count, sizeof(struct drm_amdgpu_fence),
  1073. GFP_KERNEL);
  1074. if (fences == NULL)
  1075. return -ENOMEM;
  1076. fences_user = (void __user *)(unsigned long)(wait->in.fences);
  1077. if (copy_from_user(fences, fences_user,
  1078. sizeof(struct drm_amdgpu_fence) * fence_count)) {
  1079. r = -EFAULT;
  1080. goto err_free_fences;
  1081. }
  1082. if (wait->in.wait_all)
  1083. r = amdgpu_cs_wait_all_fences(adev, filp, wait, fences);
  1084. else
  1085. r = amdgpu_cs_wait_any_fence(adev, filp, wait, fences);
  1086. err_free_fences:
  1087. kfree(fences);
  1088. return r;
  1089. }
  1090. /**
  1091. * amdgpu_cs_find_bo_va - find bo_va for VM address
  1092. *
  1093. * @parser: command submission parser context
  1094. * @addr: VM address
  1095. * @bo: resulting BO of the mapping found
  1096. *
  1097. * Search the buffer objects in the command submission context for a certain
  1098. * virtual memory address. Returns allocation structure when found, NULL
  1099. * otherwise.
  1100. */
  1101. struct amdgpu_bo_va_mapping *
  1102. amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
  1103. uint64_t addr, struct amdgpu_bo **bo)
  1104. {
  1105. struct amdgpu_bo_va_mapping *mapping;
  1106. unsigned i;
  1107. if (!parser->bo_list)
  1108. return NULL;
  1109. addr /= AMDGPU_GPU_PAGE_SIZE;
  1110. for (i = 0; i < parser->bo_list->num_entries; i++) {
  1111. struct amdgpu_bo_list_entry *lobj;
  1112. lobj = &parser->bo_list->array[i];
  1113. if (!lobj->bo_va)
  1114. continue;
  1115. list_for_each_entry(mapping, &lobj->bo_va->valids, list) {
  1116. if (mapping->it.start > addr ||
  1117. addr > mapping->it.last)
  1118. continue;
  1119. *bo = lobj->bo_va->bo;
  1120. return mapping;
  1121. }
  1122. list_for_each_entry(mapping, &lobj->bo_va->invalids, list) {
  1123. if (mapping->it.start > addr ||
  1124. addr > mapping->it.last)
  1125. continue;
  1126. *bo = lobj->bo_va->bo;
  1127. return mapping;
  1128. }
  1129. }
  1130. return NULL;
  1131. }
  1132. /**
  1133. * amdgpu_cs_sysvm_access_required - make BOs accessible by the system VM
  1134. *
  1135. * @parser: command submission parser context
  1136. *
  1137. * Helper for UVD/VCE VM emulation, make sure BOs are accessible by the system VM.
  1138. */
  1139. int amdgpu_cs_sysvm_access_required(struct amdgpu_cs_parser *parser)
  1140. {
  1141. unsigned i;
  1142. int r;
  1143. if (!parser->bo_list)
  1144. return 0;
  1145. for (i = 0; i < parser->bo_list->num_entries; i++) {
  1146. struct amdgpu_bo *bo = parser->bo_list->array[i].robj;
  1147. r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem);
  1148. if (unlikely(r))
  1149. return r;
  1150. if (bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)
  1151. continue;
  1152. bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
  1153. amdgpu_ttm_placement_from_domain(bo, bo->allowed_domains);
  1154. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
  1155. if (unlikely(r))
  1156. return r;
  1157. }
  1158. return 0;
  1159. }