amdgpu_cs.c 36 KB

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