amdgpu_cs.c 34 KB

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