amdgpu_cs.c 41 KB

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