amdgpu_cs.c 38 KB

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