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