amdgpu_cs.c 40 KB

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