amdgpu_cs.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524
  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 (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm) &&
  402. lobj->user_pages) {
  403. amdgpu_ttm_placement_from_domain(bo,
  404. AMDGPU_GEM_DOMAIN_CPU);
  405. r = ttm_bo_validate(&bo->tbo, &bo->placement, true,
  406. false);
  407. if (r)
  408. return r;
  409. amdgpu_ttm_tt_set_user_pages(bo->tbo.ttm,
  410. lobj->user_pages);
  411. binding_userptr = true;
  412. }
  413. if (p->evictable == lobj)
  414. p->evictable = NULL;
  415. r = amdgpu_cs_validate(p, bo);
  416. if (r)
  417. return r;
  418. if (binding_userptr) {
  419. kvfree(lobj->user_pages);
  420. lobj->user_pages = NULL;
  421. }
  422. }
  423. return 0;
  424. }
  425. static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
  426. union drm_amdgpu_cs *cs)
  427. {
  428. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  429. struct amdgpu_bo_list_entry *e;
  430. struct list_head duplicates;
  431. unsigned i, tries = 10;
  432. int r;
  433. INIT_LIST_HEAD(&p->validated);
  434. p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
  435. if (p->bo_list) {
  436. amdgpu_bo_list_get_list(p->bo_list, &p->validated);
  437. if (p->bo_list->first_userptr != p->bo_list->num_entries)
  438. p->mn = amdgpu_mn_get(p->adev);
  439. }
  440. INIT_LIST_HEAD(&duplicates);
  441. amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
  442. if (p->uf_entry.robj)
  443. list_add(&p->uf_entry.tv.head, &p->validated);
  444. while (1) {
  445. struct list_head need_pages;
  446. unsigned i;
  447. r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
  448. &duplicates);
  449. if (unlikely(r != 0)) {
  450. if (r != -ERESTARTSYS)
  451. DRM_ERROR("ttm_eu_reserve_buffers failed.\n");
  452. goto error_free_pages;
  453. }
  454. /* Without a BO list we don't have userptr BOs */
  455. if (!p->bo_list)
  456. break;
  457. INIT_LIST_HEAD(&need_pages);
  458. for (i = p->bo_list->first_userptr;
  459. i < p->bo_list->num_entries; ++i) {
  460. struct amdgpu_bo *bo;
  461. e = &p->bo_list->array[i];
  462. bo = e->robj;
  463. if (amdgpu_ttm_tt_userptr_invalidated(bo->tbo.ttm,
  464. &e->user_invalidated) && e->user_pages) {
  465. /* We acquired a page array, but somebody
  466. * invalidated it. Free it and try again
  467. */
  468. release_pages(e->user_pages,
  469. bo->tbo.ttm->num_pages,
  470. false);
  471. kvfree(e->user_pages);
  472. e->user_pages = NULL;
  473. }
  474. if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm) &&
  475. !e->user_pages) {
  476. list_del(&e->tv.head);
  477. list_add(&e->tv.head, &need_pages);
  478. amdgpu_bo_unreserve(e->robj);
  479. }
  480. }
  481. if (list_empty(&need_pages))
  482. break;
  483. /* Unreserve everything again. */
  484. ttm_eu_backoff_reservation(&p->ticket, &p->validated);
  485. /* We tried too many times, just abort */
  486. if (!--tries) {
  487. r = -EDEADLK;
  488. DRM_ERROR("deadlock in %s\n", __func__);
  489. goto error_free_pages;
  490. }
  491. /* Fill the page arrays for all userptrs. */
  492. list_for_each_entry(e, &need_pages, tv.head) {
  493. struct ttm_tt *ttm = e->robj->tbo.ttm;
  494. e->user_pages = kvmalloc_array(ttm->num_pages,
  495. sizeof(struct page*),
  496. GFP_KERNEL | __GFP_ZERO);
  497. if (!e->user_pages) {
  498. r = -ENOMEM;
  499. DRM_ERROR("calloc failure in %s\n", __func__);
  500. goto error_free_pages;
  501. }
  502. r = amdgpu_ttm_tt_get_user_pages(ttm, e->user_pages);
  503. if (r) {
  504. DRM_ERROR("amdgpu_ttm_tt_get_user_pages failed.\n");
  505. kvfree(e->user_pages);
  506. e->user_pages = NULL;
  507. goto error_free_pages;
  508. }
  509. }
  510. /* And try again. */
  511. list_splice(&need_pages, &p->validated);
  512. }
  513. amdgpu_cs_get_threshold_for_moves(p->adev, &p->bytes_moved_threshold,
  514. &p->bytes_moved_vis_threshold);
  515. p->bytes_moved = 0;
  516. p->bytes_moved_vis = 0;
  517. p->evictable = list_last_entry(&p->validated,
  518. struct amdgpu_bo_list_entry,
  519. tv.head);
  520. r = amdgpu_vm_validate_pt_bos(p->adev, &fpriv->vm,
  521. amdgpu_cs_validate, p);
  522. if (r) {
  523. DRM_ERROR("amdgpu_vm_validate_pt_bos() failed.\n");
  524. goto error_validate;
  525. }
  526. r = amdgpu_cs_list_validate(p, &duplicates);
  527. if (r) {
  528. DRM_ERROR("amdgpu_cs_list_validate(duplicates) failed.\n");
  529. goto error_validate;
  530. }
  531. r = amdgpu_cs_list_validate(p, &p->validated);
  532. if (r) {
  533. DRM_ERROR("amdgpu_cs_list_validate(validated) failed.\n");
  534. goto error_validate;
  535. }
  536. amdgpu_cs_report_moved_bytes(p->adev, p->bytes_moved,
  537. p->bytes_moved_vis);
  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. ttm_eu_backoff_reservation(&p->ticket, &p->validated);
  569. error_free_pages:
  570. if (p->bo_list) {
  571. for (i = p->bo_list->first_userptr;
  572. i < p->bo_list->num_entries; ++i) {
  573. e = &p->bo_list->array[i];
  574. if (!e->user_pages)
  575. continue;
  576. release_pages(e->user_pages,
  577. e->robj->tbo.ttm->num_pages,
  578. false);
  579. kvfree(e->user_pages);
  580. }
  581. }
  582. return r;
  583. }
  584. static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
  585. {
  586. struct amdgpu_bo_list_entry *e;
  587. int r;
  588. list_for_each_entry(e, &p->validated, tv.head) {
  589. struct reservation_object *resv = e->robj->tbo.resv;
  590. r = amdgpu_sync_resv(p->adev, &p->job->sync, resv, p->filp);
  591. if (r)
  592. return r;
  593. }
  594. return 0;
  595. }
  596. /**
  597. * cs_parser_fini() - clean parser states
  598. * @parser: parser structure holding parsing context.
  599. * @error: error number
  600. *
  601. * If error is set than unvalidate buffer, otherwise just free memory
  602. * used by parsing context.
  603. **/
  604. static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error,
  605. bool backoff)
  606. {
  607. unsigned i;
  608. if (error && backoff)
  609. ttm_eu_backoff_reservation(&parser->ticket,
  610. &parser->validated);
  611. for (i = 0; i < parser->num_post_dep_syncobjs; i++)
  612. drm_syncobj_put(parser->post_dep_syncobjs[i]);
  613. kfree(parser->post_dep_syncobjs);
  614. dma_fence_put(parser->fence);
  615. if (parser->ctx)
  616. amdgpu_ctx_put(parser->ctx);
  617. if (parser->bo_list)
  618. amdgpu_bo_list_put(parser->bo_list);
  619. for (i = 0; i < parser->nchunks; i++)
  620. kvfree(parser->chunks[i].kdata);
  621. kfree(parser->chunks);
  622. if (parser->job)
  623. amdgpu_job_free(parser->job);
  624. amdgpu_bo_unref(&parser->uf_entry.robj);
  625. }
  626. static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p)
  627. {
  628. struct amdgpu_device *adev = p->adev;
  629. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  630. struct amdgpu_vm *vm = &fpriv->vm;
  631. struct amdgpu_bo_va *bo_va;
  632. struct amdgpu_bo *bo;
  633. int i, r;
  634. r = amdgpu_vm_update_directories(adev, vm);
  635. if (r)
  636. return r;
  637. r = amdgpu_vm_clear_freed(adev, vm, NULL);
  638. if (r)
  639. return r;
  640. r = amdgpu_vm_bo_update(adev, fpriv->prt_va, false);
  641. if (r)
  642. return r;
  643. r = amdgpu_sync_fence(adev, &p->job->sync,
  644. fpriv->prt_va->last_pt_update);
  645. if (r)
  646. return r;
  647. if (amdgpu_sriov_vf(adev)) {
  648. struct dma_fence *f;
  649. bo_va = fpriv->csa_va;
  650. BUG_ON(!bo_va);
  651. r = amdgpu_vm_bo_update(adev, bo_va, false);
  652. if (r)
  653. return r;
  654. f = bo_va->last_pt_update;
  655. r = amdgpu_sync_fence(adev, &p->job->sync, f);
  656. if (r)
  657. return r;
  658. }
  659. if (p->bo_list) {
  660. for (i = 0; i < p->bo_list->num_entries; i++) {
  661. struct dma_fence *f;
  662. /* ignore duplicates */
  663. bo = p->bo_list->array[i].robj;
  664. if (!bo)
  665. continue;
  666. bo_va = p->bo_list->array[i].bo_va;
  667. if (bo_va == NULL)
  668. continue;
  669. r = amdgpu_vm_bo_update(adev, bo_va, false);
  670. if (r)
  671. return r;
  672. f = bo_va->last_pt_update;
  673. r = amdgpu_sync_fence(adev, &p->job->sync, f);
  674. if (r)
  675. return r;
  676. }
  677. }
  678. r = amdgpu_vm_handle_moved(adev, vm);
  679. if (r)
  680. return r;
  681. r = amdgpu_sync_fence(adev, &p->job->sync, vm->last_update);
  682. if (r)
  683. return r;
  684. if (amdgpu_vm_debug && p->bo_list) {
  685. /* Invalidate all BOs to test for userspace bugs */
  686. for (i = 0; i < p->bo_list->num_entries; i++) {
  687. /* ignore duplicates */
  688. bo = p->bo_list->array[i].robj;
  689. if (!bo)
  690. continue;
  691. amdgpu_vm_bo_invalidate(adev, bo, false);
  692. }
  693. }
  694. return r;
  695. }
  696. static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
  697. struct amdgpu_cs_parser *p)
  698. {
  699. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  700. struct amdgpu_vm *vm = &fpriv->vm;
  701. struct amdgpu_ring *ring = p->job->ring;
  702. int i, r;
  703. /* Only for UVD/VCE VM emulation */
  704. if (ring->funcs->parse_cs) {
  705. for (i = 0; i < p->job->num_ibs; i++) {
  706. r = amdgpu_ring_parse_cs(ring, p, i);
  707. if (r)
  708. return r;
  709. }
  710. }
  711. if (p->job->vm) {
  712. p->job->vm_pd_addr = amdgpu_bo_gpu_offset(vm->root.base.bo);
  713. r = amdgpu_bo_vm_update_pte(p);
  714. if (r)
  715. return r;
  716. }
  717. return amdgpu_cs_sync_rings(p);
  718. }
  719. static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
  720. struct amdgpu_cs_parser *parser)
  721. {
  722. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  723. struct amdgpu_vm *vm = &fpriv->vm;
  724. int i, j;
  725. int r, ce_preempt = 0, de_preempt = 0;
  726. for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) {
  727. struct amdgpu_cs_chunk *chunk;
  728. struct amdgpu_ib *ib;
  729. struct drm_amdgpu_cs_chunk_ib *chunk_ib;
  730. struct amdgpu_ring *ring;
  731. chunk = &parser->chunks[i];
  732. ib = &parser->job->ibs[j];
  733. chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
  734. if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
  735. continue;
  736. if (chunk_ib->ip_type == AMDGPU_HW_IP_GFX && amdgpu_sriov_vf(adev)) {
  737. if (chunk_ib->flags & AMDGPU_IB_FLAG_PREEMPT) {
  738. if (chunk_ib->flags & AMDGPU_IB_FLAG_CE)
  739. ce_preempt++;
  740. else
  741. de_preempt++;
  742. }
  743. /* each GFX command submit allows 0 or 1 IB preemptible for CE & DE */
  744. if (ce_preempt > 1 || de_preempt > 1)
  745. return -EINVAL;
  746. }
  747. r = amdgpu_queue_mgr_map(adev, &parser->ctx->queue_mgr, chunk_ib->ip_type,
  748. chunk_ib->ip_instance, chunk_ib->ring, &ring);
  749. if (r)
  750. return r;
  751. if (chunk_ib->flags & AMDGPU_IB_FLAG_PREAMBLE) {
  752. parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT;
  753. if (!parser->ctx->preamble_presented) {
  754. parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT_FIRST;
  755. parser->ctx->preamble_presented = true;
  756. }
  757. }
  758. if (parser->job->ring && parser->job->ring != ring)
  759. return -EINVAL;
  760. parser->job->ring = ring;
  761. if (ring->funcs->parse_cs) {
  762. struct amdgpu_bo_va_mapping *m;
  763. struct amdgpu_bo *aobj = NULL;
  764. uint64_t offset;
  765. uint8_t *kptr;
  766. r = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
  767. &aobj, &m);
  768. if (r) {
  769. DRM_ERROR("IB va_start is invalid\n");
  770. return r;
  771. }
  772. if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
  773. (m->last + 1) * AMDGPU_GPU_PAGE_SIZE) {
  774. DRM_ERROR("IB va_start+ib_bytes is invalid\n");
  775. return -EINVAL;
  776. }
  777. /* the IB should be reserved at this point */
  778. r = amdgpu_bo_kmap(aobj, (void **)&kptr);
  779. if (r) {
  780. return r;
  781. }
  782. offset = m->start * AMDGPU_GPU_PAGE_SIZE;
  783. kptr += chunk_ib->va_start - offset;
  784. r = amdgpu_ib_get(adev, vm, chunk_ib->ib_bytes, ib);
  785. if (r) {
  786. DRM_ERROR("Failed to get ib !\n");
  787. return r;
  788. }
  789. memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
  790. amdgpu_bo_kunmap(aobj);
  791. } else {
  792. r = amdgpu_ib_get(adev, vm, 0, ib);
  793. if (r) {
  794. DRM_ERROR("Failed to get ib !\n");
  795. return r;
  796. }
  797. }
  798. ib->gpu_addr = chunk_ib->va_start;
  799. ib->length_dw = chunk_ib->ib_bytes / 4;
  800. ib->flags = chunk_ib->flags;
  801. j++;
  802. }
  803. /* UVD & VCE fw doesn't support user fences */
  804. if (parser->job->uf_addr && (
  805. parser->job->ring->funcs->type == AMDGPU_RING_TYPE_UVD ||
  806. parser->job->ring->funcs->type == AMDGPU_RING_TYPE_VCE))
  807. return -EINVAL;
  808. return 0;
  809. }
  810. static int amdgpu_cs_process_fence_dep(struct amdgpu_cs_parser *p,
  811. struct amdgpu_cs_chunk *chunk)
  812. {
  813. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  814. unsigned num_deps;
  815. int i, r;
  816. struct drm_amdgpu_cs_chunk_dep *deps;
  817. deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
  818. num_deps = chunk->length_dw * 4 /
  819. sizeof(struct drm_amdgpu_cs_chunk_dep);
  820. for (i = 0; i < num_deps; ++i) {
  821. struct amdgpu_ring *ring;
  822. struct amdgpu_ctx *ctx;
  823. struct dma_fence *fence;
  824. ctx = amdgpu_ctx_get(fpriv, deps[i].ctx_id);
  825. if (ctx == NULL)
  826. return -EINVAL;
  827. r = amdgpu_queue_mgr_map(p->adev, &ctx->queue_mgr,
  828. deps[i].ip_type,
  829. deps[i].ip_instance,
  830. deps[i].ring, &ring);
  831. if (r) {
  832. amdgpu_ctx_put(ctx);
  833. return r;
  834. }
  835. fence = amdgpu_ctx_get_fence(ctx, ring,
  836. deps[i].handle);
  837. if (IS_ERR(fence)) {
  838. r = PTR_ERR(fence);
  839. amdgpu_ctx_put(ctx);
  840. return r;
  841. } else if (fence) {
  842. r = amdgpu_sync_fence(p->adev, &p->job->sync,
  843. fence);
  844. dma_fence_put(fence);
  845. amdgpu_ctx_put(ctx);
  846. if (r)
  847. return r;
  848. }
  849. }
  850. return 0;
  851. }
  852. static int amdgpu_syncobj_lookup_and_add_to_sync(struct amdgpu_cs_parser *p,
  853. uint32_t handle)
  854. {
  855. int r;
  856. struct dma_fence *fence;
  857. r = drm_syncobj_find_fence(p->filp, handle, &fence);
  858. if (r)
  859. return r;
  860. r = amdgpu_sync_fence(p->adev, &p->job->sync, fence);
  861. dma_fence_put(fence);
  862. return r;
  863. }
  864. static int amdgpu_cs_process_syncobj_in_dep(struct amdgpu_cs_parser *p,
  865. struct amdgpu_cs_chunk *chunk)
  866. {
  867. unsigned num_deps;
  868. int i, r;
  869. struct drm_amdgpu_cs_chunk_sem *deps;
  870. deps = (struct drm_amdgpu_cs_chunk_sem *)chunk->kdata;
  871. num_deps = chunk->length_dw * 4 /
  872. sizeof(struct drm_amdgpu_cs_chunk_sem);
  873. for (i = 0; i < num_deps; ++i) {
  874. r = amdgpu_syncobj_lookup_and_add_to_sync(p, deps[i].handle);
  875. if (r)
  876. return r;
  877. }
  878. return 0;
  879. }
  880. static int amdgpu_cs_process_syncobj_out_dep(struct amdgpu_cs_parser *p,
  881. struct amdgpu_cs_chunk *chunk)
  882. {
  883. unsigned num_deps;
  884. int i;
  885. struct drm_amdgpu_cs_chunk_sem *deps;
  886. deps = (struct drm_amdgpu_cs_chunk_sem *)chunk->kdata;
  887. num_deps = chunk->length_dw * 4 /
  888. sizeof(struct drm_amdgpu_cs_chunk_sem);
  889. p->post_dep_syncobjs = kmalloc_array(num_deps,
  890. sizeof(struct drm_syncobj *),
  891. GFP_KERNEL);
  892. p->num_post_dep_syncobjs = 0;
  893. if (!p->post_dep_syncobjs)
  894. return -ENOMEM;
  895. for (i = 0; i < num_deps; ++i) {
  896. p->post_dep_syncobjs[i] = drm_syncobj_find(p->filp, deps[i].handle);
  897. if (!p->post_dep_syncobjs[i])
  898. return -EINVAL;
  899. p->num_post_dep_syncobjs++;
  900. }
  901. return 0;
  902. }
  903. static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
  904. struct amdgpu_cs_parser *p)
  905. {
  906. int i, r;
  907. for (i = 0; i < p->nchunks; ++i) {
  908. struct amdgpu_cs_chunk *chunk;
  909. chunk = &p->chunks[i];
  910. if (chunk->chunk_id == AMDGPU_CHUNK_ID_DEPENDENCIES) {
  911. r = amdgpu_cs_process_fence_dep(p, chunk);
  912. if (r)
  913. return r;
  914. } else if (chunk->chunk_id == AMDGPU_CHUNK_ID_SYNCOBJ_IN) {
  915. r = amdgpu_cs_process_syncobj_in_dep(p, chunk);
  916. if (r)
  917. return r;
  918. } else if (chunk->chunk_id == AMDGPU_CHUNK_ID_SYNCOBJ_OUT) {
  919. r = amdgpu_cs_process_syncobj_out_dep(p, chunk);
  920. if (r)
  921. return r;
  922. }
  923. }
  924. return 0;
  925. }
  926. static void amdgpu_cs_post_dependencies(struct amdgpu_cs_parser *p)
  927. {
  928. int i;
  929. for (i = 0; i < p->num_post_dep_syncobjs; ++i)
  930. drm_syncobj_replace_fence(p->post_dep_syncobjs[i], p->fence);
  931. }
  932. static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
  933. union drm_amdgpu_cs *cs)
  934. {
  935. struct amdgpu_ring *ring = p->job->ring;
  936. struct amd_sched_entity *entity = &p->ctx->rings[ring->idx].entity;
  937. struct amdgpu_job *job;
  938. unsigned i;
  939. uint64_t seq;
  940. int r;
  941. amdgpu_mn_lock(p->mn);
  942. if (p->bo_list) {
  943. for (i = p->bo_list->first_userptr;
  944. i < p->bo_list->num_entries; ++i) {
  945. struct amdgpu_bo *bo = p->bo_list->array[i].robj;
  946. if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm)) {
  947. amdgpu_mn_unlock(p->mn);
  948. return -ERESTARTSYS;
  949. }
  950. }
  951. }
  952. job = p->job;
  953. p->job = NULL;
  954. r = amd_sched_job_init(&job->base, &ring->sched, entity, p->filp);
  955. if (r) {
  956. amdgpu_job_free(job);
  957. amdgpu_mn_unlock(p->mn);
  958. return r;
  959. }
  960. job->owner = p->filp;
  961. job->fence_ctx = entity->fence_context;
  962. p->fence = dma_fence_get(&job->base.s_fence->finished);
  963. r = amdgpu_ctx_add_fence(p->ctx, ring, p->fence, &seq);
  964. if (r) {
  965. dma_fence_put(p->fence);
  966. dma_fence_put(&job->base.s_fence->finished);
  967. amdgpu_job_free(job);
  968. amdgpu_mn_unlock(p->mn);
  969. return r;
  970. }
  971. amdgpu_cs_post_dependencies(p);
  972. cs->out.handle = seq;
  973. job->uf_sequence = seq;
  974. amdgpu_job_free_resources(job);
  975. trace_amdgpu_cs_ioctl(job);
  976. amd_sched_entity_push_job(&job->base);
  977. ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
  978. amdgpu_mn_unlock(p->mn);
  979. return 0;
  980. }
  981. int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  982. {
  983. struct amdgpu_device *adev = dev->dev_private;
  984. struct amdgpu_fpriv *fpriv = filp->driver_priv;
  985. union drm_amdgpu_cs *cs = data;
  986. struct amdgpu_cs_parser parser = {};
  987. bool reserved_buffers = false;
  988. int i, r;
  989. if (!adev->accel_working)
  990. return -EBUSY;
  991. if (amdgpu_kms_vram_lost(adev, fpriv))
  992. return -ENODEV;
  993. parser.adev = adev;
  994. parser.filp = filp;
  995. r = amdgpu_cs_parser_init(&parser, data);
  996. if (r) {
  997. DRM_ERROR("Failed to initialize parser !\n");
  998. goto out;
  999. }
  1000. r = amdgpu_cs_parser_bos(&parser, data);
  1001. if (r) {
  1002. if (r == -ENOMEM)
  1003. DRM_ERROR("Not enough memory for command submission!\n");
  1004. else if (r != -ERESTARTSYS)
  1005. DRM_ERROR("Failed to process the buffer list %d!\n", r);
  1006. goto out;
  1007. }
  1008. reserved_buffers = true;
  1009. r = amdgpu_cs_ib_fill(adev, &parser);
  1010. if (r)
  1011. goto out;
  1012. r = amdgpu_cs_dependencies(adev, &parser);
  1013. if (r) {
  1014. DRM_ERROR("Failed in the dependencies handling %d!\n", r);
  1015. goto out;
  1016. }
  1017. for (i = 0; i < parser.job->num_ibs; i++)
  1018. trace_amdgpu_cs(&parser, i);
  1019. r = amdgpu_cs_ib_vm_chunk(adev, &parser);
  1020. if (r)
  1021. goto out;
  1022. r = amdgpu_cs_submit(&parser, cs);
  1023. out:
  1024. amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
  1025. return r;
  1026. }
  1027. /**
  1028. * amdgpu_cs_wait_ioctl - wait for a command submission to finish
  1029. *
  1030. * @dev: drm device
  1031. * @data: data from userspace
  1032. * @filp: file private
  1033. *
  1034. * Wait for the command submission identified by handle to finish.
  1035. */
  1036. int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
  1037. struct drm_file *filp)
  1038. {
  1039. union drm_amdgpu_wait_cs *wait = data;
  1040. struct amdgpu_device *adev = dev->dev_private;
  1041. struct amdgpu_fpriv *fpriv = filp->driver_priv;
  1042. unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
  1043. struct amdgpu_ring *ring = NULL;
  1044. struct amdgpu_ctx *ctx;
  1045. struct dma_fence *fence;
  1046. long r;
  1047. if (amdgpu_kms_vram_lost(adev, fpriv))
  1048. return -ENODEV;
  1049. ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
  1050. if (ctx == NULL)
  1051. return -EINVAL;
  1052. r = amdgpu_queue_mgr_map(adev, &ctx->queue_mgr,
  1053. wait->in.ip_type, wait->in.ip_instance,
  1054. wait->in.ring, &ring);
  1055. if (r) {
  1056. amdgpu_ctx_put(ctx);
  1057. return r;
  1058. }
  1059. fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
  1060. if (IS_ERR(fence))
  1061. r = PTR_ERR(fence);
  1062. else if (fence) {
  1063. r = dma_fence_wait_timeout(fence, true, timeout);
  1064. dma_fence_put(fence);
  1065. } else
  1066. r = 1;
  1067. amdgpu_ctx_put(ctx);
  1068. if (r < 0)
  1069. return r;
  1070. memset(wait, 0, sizeof(*wait));
  1071. wait->out.status = (r == 0);
  1072. return 0;
  1073. }
  1074. /**
  1075. * amdgpu_cs_get_fence - helper to get fence from drm_amdgpu_fence
  1076. *
  1077. * @adev: amdgpu device
  1078. * @filp: file private
  1079. * @user: drm_amdgpu_fence copied from user space
  1080. */
  1081. static struct dma_fence *amdgpu_cs_get_fence(struct amdgpu_device *adev,
  1082. struct drm_file *filp,
  1083. struct drm_amdgpu_fence *user)
  1084. {
  1085. struct amdgpu_ring *ring;
  1086. struct amdgpu_ctx *ctx;
  1087. struct dma_fence *fence;
  1088. int r;
  1089. ctx = amdgpu_ctx_get(filp->driver_priv, user->ctx_id);
  1090. if (ctx == NULL)
  1091. return ERR_PTR(-EINVAL);
  1092. r = amdgpu_queue_mgr_map(adev, &ctx->queue_mgr, user->ip_type,
  1093. user->ip_instance, user->ring, &ring);
  1094. if (r) {
  1095. amdgpu_ctx_put(ctx);
  1096. return ERR_PTR(r);
  1097. }
  1098. fence = amdgpu_ctx_get_fence(ctx, ring, user->seq_no);
  1099. amdgpu_ctx_put(ctx);
  1100. return fence;
  1101. }
  1102. /**
  1103. * amdgpu_cs_wait_all_fence - wait on all fences to signal
  1104. *
  1105. * @adev: amdgpu device
  1106. * @filp: file private
  1107. * @wait: wait parameters
  1108. * @fences: array of drm_amdgpu_fence
  1109. */
  1110. static int amdgpu_cs_wait_all_fences(struct amdgpu_device *adev,
  1111. struct drm_file *filp,
  1112. union drm_amdgpu_wait_fences *wait,
  1113. struct drm_amdgpu_fence *fences)
  1114. {
  1115. uint32_t fence_count = wait->in.fence_count;
  1116. unsigned int i;
  1117. long r = 1;
  1118. for (i = 0; i < fence_count; i++) {
  1119. struct dma_fence *fence;
  1120. unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
  1121. fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
  1122. if (IS_ERR(fence))
  1123. return PTR_ERR(fence);
  1124. else if (!fence)
  1125. continue;
  1126. r = dma_fence_wait_timeout(fence, true, timeout);
  1127. dma_fence_put(fence);
  1128. if (r < 0)
  1129. return r;
  1130. if (r == 0)
  1131. break;
  1132. }
  1133. memset(wait, 0, sizeof(*wait));
  1134. wait->out.status = (r > 0);
  1135. return 0;
  1136. }
  1137. /**
  1138. * amdgpu_cs_wait_any_fence - wait on any fence to signal
  1139. *
  1140. * @adev: amdgpu device
  1141. * @filp: file private
  1142. * @wait: wait parameters
  1143. * @fences: array of drm_amdgpu_fence
  1144. */
  1145. static int amdgpu_cs_wait_any_fence(struct amdgpu_device *adev,
  1146. struct drm_file *filp,
  1147. union drm_amdgpu_wait_fences *wait,
  1148. struct drm_amdgpu_fence *fences)
  1149. {
  1150. unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
  1151. uint32_t fence_count = wait->in.fence_count;
  1152. uint32_t first = ~0;
  1153. struct dma_fence **array;
  1154. unsigned int i;
  1155. long r;
  1156. /* Prepare the fence array */
  1157. array = kcalloc(fence_count, sizeof(struct dma_fence *), GFP_KERNEL);
  1158. if (array == NULL)
  1159. return -ENOMEM;
  1160. for (i = 0; i < fence_count; i++) {
  1161. struct dma_fence *fence;
  1162. fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
  1163. if (IS_ERR(fence)) {
  1164. r = PTR_ERR(fence);
  1165. goto err_free_fence_array;
  1166. } else if (fence) {
  1167. array[i] = fence;
  1168. } else { /* NULL, the fence has been already signaled */
  1169. r = 1;
  1170. first = i;
  1171. goto out;
  1172. }
  1173. }
  1174. r = dma_fence_wait_any_timeout(array, fence_count, true, timeout,
  1175. &first);
  1176. if (r < 0)
  1177. goto err_free_fence_array;
  1178. out:
  1179. memset(wait, 0, sizeof(*wait));
  1180. wait->out.status = (r > 0);
  1181. wait->out.first_signaled = first;
  1182. /* set return value 0 to indicate success */
  1183. r = 0;
  1184. err_free_fence_array:
  1185. for (i = 0; i < fence_count; i++)
  1186. dma_fence_put(array[i]);
  1187. kfree(array);
  1188. return r;
  1189. }
  1190. /**
  1191. * amdgpu_cs_wait_fences_ioctl - wait for multiple command submissions to finish
  1192. *
  1193. * @dev: drm device
  1194. * @data: data from userspace
  1195. * @filp: file private
  1196. */
  1197. int amdgpu_cs_wait_fences_ioctl(struct drm_device *dev, void *data,
  1198. struct drm_file *filp)
  1199. {
  1200. struct amdgpu_device *adev = dev->dev_private;
  1201. struct amdgpu_fpriv *fpriv = filp->driver_priv;
  1202. union drm_amdgpu_wait_fences *wait = data;
  1203. uint32_t fence_count = wait->in.fence_count;
  1204. struct drm_amdgpu_fence *fences_user;
  1205. struct drm_amdgpu_fence *fences;
  1206. int r;
  1207. if (amdgpu_kms_vram_lost(adev, fpriv))
  1208. return -ENODEV;
  1209. /* Get the fences from userspace */
  1210. fences = kmalloc_array(fence_count, sizeof(struct drm_amdgpu_fence),
  1211. GFP_KERNEL);
  1212. if (fences == NULL)
  1213. return -ENOMEM;
  1214. fences_user = u64_to_user_ptr(wait->in.fences);
  1215. if (copy_from_user(fences, fences_user,
  1216. sizeof(struct drm_amdgpu_fence) * fence_count)) {
  1217. r = -EFAULT;
  1218. goto err_free_fences;
  1219. }
  1220. if (wait->in.wait_all)
  1221. r = amdgpu_cs_wait_all_fences(adev, filp, wait, fences);
  1222. else
  1223. r = amdgpu_cs_wait_any_fence(adev, filp, wait, fences);
  1224. err_free_fences:
  1225. kfree(fences);
  1226. return r;
  1227. }
  1228. /**
  1229. * amdgpu_cs_find_bo_va - find bo_va for VM address
  1230. *
  1231. * @parser: command submission parser context
  1232. * @addr: VM address
  1233. * @bo: resulting BO of the mapping found
  1234. *
  1235. * Search the buffer objects in the command submission context for a certain
  1236. * virtual memory address. Returns allocation structure when found, NULL
  1237. * otherwise.
  1238. */
  1239. int amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
  1240. uint64_t addr, struct amdgpu_bo **bo,
  1241. struct amdgpu_bo_va_mapping **map)
  1242. {
  1243. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  1244. struct amdgpu_vm *vm = &fpriv->vm;
  1245. struct amdgpu_bo_va_mapping *mapping;
  1246. int r;
  1247. addr /= AMDGPU_GPU_PAGE_SIZE;
  1248. mapping = amdgpu_vm_bo_lookup_mapping(vm, addr);
  1249. if (!mapping || !mapping->bo_va || !mapping->bo_va->base.bo)
  1250. return -EINVAL;
  1251. *bo = mapping->bo_va->base.bo;
  1252. *map = mapping;
  1253. /* Double check that the BO is reserved by this CS */
  1254. if (READ_ONCE((*bo)->tbo.resv->lock.ctx) != &parser->ticket)
  1255. return -EINVAL;
  1256. r = amdgpu_ttm_bind(&(*bo)->tbo, &(*bo)->tbo.mem);
  1257. if (unlikely(r))
  1258. return r;
  1259. if ((*bo)->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)
  1260. return 0;
  1261. (*bo)->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
  1262. amdgpu_ttm_placement_from_domain(*bo, (*bo)->allowed_domains);
  1263. return ttm_bo_validate(&(*bo)->tbo, &(*bo)->placement, false, false);
  1264. }