amdgpu_cs.c 39 KB

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