amdgpu_cs.c 34 KB

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