amdgpu_cs.c 39 KB

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