amdgpu_cs.c 39 KB

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