amdgpu_cs.c 34 KB

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