amdgpu_cs.c 39 KB

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