amdgpu_cs.c 40 KB

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