amdgpu_cs.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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 <drm/drmP.h>
  29. #include <drm/amdgpu_drm.h>
  30. #include "amdgpu.h"
  31. #include "amdgpu_trace.h"
  32. int amdgpu_cs_get_ring(struct amdgpu_device *adev, u32 ip_type,
  33. u32 ip_instance, u32 ring,
  34. struct amdgpu_ring **out_ring)
  35. {
  36. /* Right now all IPs have only one instance - multiple rings. */
  37. if (ip_instance != 0) {
  38. DRM_ERROR("invalid ip instance: %d\n", ip_instance);
  39. return -EINVAL;
  40. }
  41. switch (ip_type) {
  42. default:
  43. DRM_ERROR("unknown ip type: %d\n", ip_type);
  44. return -EINVAL;
  45. case AMDGPU_HW_IP_GFX:
  46. if (ring < adev->gfx.num_gfx_rings) {
  47. *out_ring = &adev->gfx.gfx_ring[ring];
  48. } else {
  49. DRM_ERROR("only %d gfx rings are supported now\n",
  50. adev->gfx.num_gfx_rings);
  51. return -EINVAL;
  52. }
  53. break;
  54. case AMDGPU_HW_IP_COMPUTE:
  55. if (ring < adev->gfx.num_compute_rings) {
  56. *out_ring = &adev->gfx.compute_ring[ring];
  57. } else {
  58. DRM_ERROR("only %d compute rings are supported now\n",
  59. adev->gfx.num_compute_rings);
  60. return -EINVAL;
  61. }
  62. break;
  63. case AMDGPU_HW_IP_DMA:
  64. if (ring < adev->sdma.num_instances) {
  65. *out_ring = &adev->sdma.instance[ring].ring;
  66. } else {
  67. DRM_ERROR("only %d SDMA rings are supported\n",
  68. adev->sdma.num_instances);
  69. return -EINVAL;
  70. }
  71. break;
  72. case AMDGPU_HW_IP_UVD:
  73. *out_ring = &adev->uvd.ring;
  74. break;
  75. case AMDGPU_HW_IP_VCE:
  76. if (ring < 2){
  77. *out_ring = &adev->vce.ring[ring];
  78. } else {
  79. DRM_ERROR("only two VCE rings are supported\n");
  80. return -EINVAL;
  81. }
  82. break;
  83. }
  84. return 0;
  85. }
  86. static int amdgpu_cs_user_fence_chunk(struct amdgpu_cs_parser *p,
  87. struct drm_amdgpu_cs_chunk_fence *data,
  88. uint32_t *offset)
  89. {
  90. struct drm_gem_object *gobj;
  91. unsigned long size;
  92. gobj = drm_gem_object_lookup(p->filp, data->handle);
  93. if (gobj == NULL)
  94. return -EINVAL;
  95. p->uf_entry.robj = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
  96. p->uf_entry.priority = 0;
  97. p->uf_entry.tv.bo = &p->uf_entry.robj->tbo;
  98. p->uf_entry.tv.shared = true;
  99. p->uf_entry.user_pages = NULL;
  100. size = amdgpu_bo_size(p->uf_entry.robj);
  101. if (size != PAGE_SIZE || (data->offset + 8) > size)
  102. return -EINVAL;
  103. *offset = data->offset;
  104. drm_gem_object_unreference_unlocked(gobj);
  105. if (amdgpu_ttm_tt_get_usermm(p->uf_entry.robj->tbo.ttm)) {
  106. amdgpu_bo_unref(&p->uf_entry.robj);
  107. return -EINVAL;
  108. }
  109. return 0;
  110. }
  111. int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, void *data)
  112. {
  113. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  114. struct amdgpu_vm *vm = &fpriv->vm;
  115. union drm_amdgpu_cs *cs = data;
  116. uint64_t *chunk_array_user;
  117. uint64_t *chunk_array;
  118. unsigned size, num_ibs = 0;
  119. uint32_t uf_offset = 0;
  120. int i;
  121. int ret;
  122. if (cs->in.num_chunks == 0)
  123. return 0;
  124. chunk_array = kmalloc_array(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL);
  125. if (!chunk_array)
  126. return -ENOMEM;
  127. p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id);
  128. if (!p->ctx) {
  129. ret = -EINVAL;
  130. goto free_chunk;
  131. }
  132. /* get chunks */
  133. chunk_array_user = (uint64_t __user *)(unsigned long)(cs->in.chunks);
  134. if (copy_from_user(chunk_array, chunk_array_user,
  135. sizeof(uint64_t)*cs->in.num_chunks)) {
  136. ret = -EFAULT;
  137. goto put_ctx;
  138. }
  139. p->nchunks = cs->in.num_chunks;
  140. p->chunks = kmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk),
  141. GFP_KERNEL);
  142. if (!p->chunks) {
  143. ret = -ENOMEM;
  144. goto put_ctx;
  145. }
  146. for (i = 0; i < p->nchunks; i++) {
  147. struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL;
  148. struct drm_amdgpu_cs_chunk user_chunk;
  149. uint32_t __user *cdata;
  150. chunk_ptr = (void __user *)(unsigned long)chunk_array[i];
  151. if (copy_from_user(&user_chunk, chunk_ptr,
  152. sizeof(struct drm_amdgpu_cs_chunk))) {
  153. ret = -EFAULT;
  154. i--;
  155. goto free_partial_kdata;
  156. }
  157. p->chunks[i].chunk_id = user_chunk.chunk_id;
  158. p->chunks[i].length_dw = user_chunk.length_dw;
  159. size = p->chunks[i].length_dw;
  160. cdata = (void __user *)(unsigned long)user_chunk.chunk_data;
  161. p->chunks[i].kdata = drm_malloc_ab(size, sizeof(uint32_t));
  162. if (p->chunks[i].kdata == NULL) {
  163. ret = -ENOMEM;
  164. i--;
  165. goto free_partial_kdata;
  166. }
  167. size *= sizeof(uint32_t);
  168. if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
  169. ret = -EFAULT;
  170. goto free_partial_kdata;
  171. }
  172. switch (p->chunks[i].chunk_id) {
  173. case AMDGPU_CHUNK_ID_IB:
  174. ++num_ibs;
  175. break;
  176. case AMDGPU_CHUNK_ID_FENCE:
  177. size = sizeof(struct drm_amdgpu_cs_chunk_fence);
  178. if (p->chunks[i].length_dw * sizeof(uint32_t) < size) {
  179. ret = -EINVAL;
  180. goto free_partial_kdata;
  181. }
  182. ret = amdgpu_cs_user_fence_chunk(p, p->chunks[i].kdata,
  183. &uf_offset);
  184. if (ret)
  185. goto free_partial_kdata;
  186. break;
  187. case AMDGPU_CHUNK_ID_DEPENDENCIES:
  188. break;
  189. default:
  190. ret = -EINVAL;
  191. goto free_partial_kdata;
  192. }
  193. }
  194. ret = amdgpu_job_alloc(p->adev, num_ibs, &p->job, vm);
  195. if (ret)
  196. goto free_all_kdata;
  197. if (p->uf_entry.robj)
  198. p->job->uf_addr = uf_offset;
  199. kfree(chunk_array);
  200. return 0;
  201. free_all_kdata:
  202. i = p->nchunks - 1;
  203. free_partial_kdata:
  204. for (; i >= 0; i--)
  205. drm_free_large(p->chunks[i].kdata);
  206. kfree(p->chunks);
  207. put_ctx:
  208. amdgpu_ctx_put(p->ctx);
  209. free_chunk:
  210. kfree(chunk_array);
  211. return ret;
  212. }
  213. /* Convert microseconds to bytes. */
  214. static u64 us_to_bytes(struct amdgpu_device *adev, s64 us)
  215. {
  216. if (us <= 0 || !adev->mm_stats.log2_max_MBps)
  217. return 0;
  218. /* Since accum_us is incremented by a million per second, just
  219. * multiply it by the number of MB/s to get the number of bytes.
  220. */
  221. return us << adev->mm_stats.log2_max_MBps;
  222. }
  223. static s64 bytes_to_us(struct amdgpu_device *adev, u64 bytes)
  224. {
  225. if (!adev->mm_stats.log2_max_MBps)
  226. return 0;
  227. return bytes >> adev->mm_stats.log2_max_MBps;
  228. }
  229. /* Returns how many bytes TTM can move right now. If no bytes can be moved,
  230. * it returns 0. If it returns non-zero, it's OK to move at least one buffer,
  231. * which means it can go over the threshold once. If that happens, the driver
  232. * will be in debt and no other buffer migrations can be done until that debt
  233. * is repaid.
  234. *
  235. * This approach allows moving a buffer of any size (it's important to allow
  236. * that).
  237. *
  238. * The currency is simply time in microseconds and it increases as the clock
  239. * ticks. The accumulated microseconds (us) are converted to bytes and
  240. * returned.
  241. */
  242. static u64 amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev)
  243. {
  244. s64 time_us, increment_us;
  245. u64 max_bytes;
  246. u64 free_vram, total_vram, used_vram;
  247. /* Allow a maximum of 200 accumulated ms. This is basically per-IB
  248. * throttling.
  249. *
  250. * It means that in order to get full max MBps, at least 5 IBs per
  251. * second must be submitted and not more than 200ms apart from each
  252. * other.
  253. */
  254. const s64 us_upper_bound = 200000;
  255. if (!adev->mm_stats.log2_max_MBps)
  256. return 0;
  257. total_vram = adev->mc.real_vram_size - adev->vram_pin_size;
  258. used_vram = atomic64_read(&adev->vram_usage);
  259. free_vram = used_vram >= total_vram ? 0 : total_vram - used_vram;
  260. spin_lock(&adev->mm_stats.lock);
  261. /* Increase the amount of accumulated us. */
  262. time_us = ktime_to_us(ktime_get());
  263. increment_us = time_us - adev->mm_stats.last_update_us;
  264. adev->mm_stats.last_update_us = time_us;
  265. adev->mm_stats.accum_us = min(adev->mm_stats.accum_us + increment_us,
  266. us_upper_bound);
  267. /* This prevents the short period of low performance when the VRAM
  268. * usage is low and the driver is in debt or doesn't have enough
  269. * accumulated us to fill VRAM quickly.
  270. *
  271. * The situation can occur in these cases:
  272. * - a lot of VRAM is freed by userspace
  273. * - the presence of a big buffer causes a lot of evictions
  274. * (solution: split buffers into smaller ones)
  275. *
  276. * If 128 MB or 1/8th of VRAM is free, start filling it now by setting
  277. * accum_us to a positive number.
  278. */
  279. if (free_vram >= 128 * 1024 * 1024 || free_vram >= total_vram / 8) {
  280. s64 min_us;
  281. /* Be more aggresive on dGPUs. Try to fill a portion of free
  282. * VRAM now.
  283. */
  284. if (!(adev->flags & AMD_IS_APU))
  285. min_us = bytes_to_us(adev, free_vram / 4);
  286. else
  287. min_us = 0; /* Reset accum_us on APUs. */
  288. adev->mm_stats.accum_us = max(min_us, adev->mm_stats.accum_us);
  289. }
  290. /* This returns 0 if the driver is in debt to disallow (optional)
  291. * buffer moves.
  292. */
  293. max_bytes = us_to_bytes(adev, adev->mm_stats.accum_us);
  294. spin_unlock(&adev->mm_stats.lock);
  295. return max_bytes;
  296. }
  297. /* Report how many bytes have really been moved for the last command
  298. * submission. This can result in a debt that can stop buffer migrations
  299. * temporarily.
  300. */
  301. static void amdgpu_cs_report_moved_bytes(struct amdgpu_device *adev,
  302. u64 num_bytes)
  303. {
  304. spin_lock(&adev->mm_stats.lock);
  305. adev->mm_stats.accum_us -= bytes_to_us(adev, num_bytes);
  306. spin_unlock(&adev->mm_stats.lock);
  307. }
  308. static int amdgpu_cs_bo_validate(struct amdgpu_cs_parser *p,
  309. struct amdgpu_bo *bo)
  310. {
  311. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  312. u64 initial_bytes_moved;
  313. uint32_t domain;
  314. int r;
  315. if (bo->pin_count)
  316. return 0;
  317. /* Don't move this buffer if we have depleted our allowance
  318. * to move it. Don't move anything if the threshold is zero.
  319. */
  320. if (p->bytes_moved < p->bytes_moved_threshold)
  321. domain = bo->prefered_domains;
  322. else
  323. domain = bo->allowed_domains;
  324. retry:
  325. amdgpu_ttm_placement_from_domain(bo, domain);
  326. initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
  327. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  328. p->bytes_moved += atomic64_read(&adev->num_bytes_moved) -
  329. initial_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_list_entry *lobj)
  339. {
  340. uint32_t domain = lobj->robj->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;
  350. uint32_t other;
  351. /* If we reached our current BO we can forget it */
  352. if (candidate == lobj)
  353. break;
  354. other = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
  355. /* Check if this BO is in one of the domains we need space for */
  356. if (!(other & domain))
  357. continue;
  358. /* Check if we can move this BO somewhere else */
  359. other = bo->allowed_domains & ~domain;
  360. if (!other)
  361. continue;
  362. /* Good we can try to move this BO somewhere else */
  363. amdgpu_ttm_placement_from_domain(bo, other);
  364. initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
  365. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  366. p->bytes_moved += atomic64_read(&adev->num_bytes_moved) -
  367. initial_bytes_moved;
  368. if (unlikely(r))
  369. break;
  370. p->evictable = list_prev_entry(p->evictable, tv.head);
  371. list_move(&candidate->tv.head, &p->validated);
  372. return true;
  373. }
  374. return false;
  375. }
  376. static int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p,
  377. struct list_head *validated)
  378. {
  379. struct amdgpu_bo_list_entry *lobj;
  380. int r;
  381. list_for_each_entry(lobj, validated, tv.head) {
  382. struct amdgpu_bo *bo = lobj->robj;
  383. bool binding_userptr = false;
  384. struct mm_struct *usermm;
  385. usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
  386. if (usermm && usermm != current->mm)
  387. return -EPERM;
  388. /* Check if we have user pages and nobody bound the BO already */
  389. if (lobj->user_pages && bo->tbo.ttm->state != tt_bound) {
  390. size_t size = sizeof(struct page *);
  391. size *= bo->tbo.ttm->num_pages;
  392. memcpy(bo->tbo.ttm->pages, lobj->user_pages, size);
  393. binding_userptr = true;
  394. }
  395. if (p->evictable == lobj)
  396. p->evictable = NULL;
  397. do {
  398. r = amdgpu_cs_bo_validate(p, bo);
  399. } while (r == -ENOMEM && amdgpu_cs_try_evict(p, lobj));
  400. if (r)
  401. return r;
  402. if (bo->shadow) {
  403. r = amdgpu_cs_bo_validate(p, bo);
  404. if (r)
  405. return r;
  406. }
  407. if (binding_userptr) {
  408. drm_free_large(lobj->user_pages);
  409. lobj->user_pages = NULL;
  410. }
  411. }
  412. return 0;
  413. }
  414. static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
  415. union drm_amdgpu_cs *cs)
  416. {
  417. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  418. struct amdgpu_bo_list_entry *e;
  419. struct list_head duplicates;
  420. bool need_mmap_lock = false;
  421. unsigned i, tries = 10;
  422. int r;
  423. INIT_LIST_HEAD(&p->validated);
  424. p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
  425. if (p->bo_list) {
  426. need_mmap_lock = p->bo_list->first_userptr !=
  427. p->bo_list->num_entries;
  428. amdgpu_bo_list_get_list(p->bo_list, &p->validated);
  429. }
  430. INIT_LIST_HEAD(&duplicates);
  431. amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
  432. if (p->uf_entry.robj)
  433. list_add(&p->uf_entry.tv.head, &p->validated);
  434. if (need_mmap_lock)
  435. down_read(&current->mm->mmap_sem);
  436. while (1) {
  437. struct list_head need_pages;
  438. unsigned i;
  439. r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
  440. &duplicates);
  441. if (unlikely(r != 0)) {
  442. DRM_ERROR("ttm_eu_reserve_buffers failed.\n");
  443. goto error_free_pages;
  444. }
  445. /* Without a BO list we don't have userptr BOs */
  446. if (!p->bo_list)
  447. break;
  448. INIT_LIST_HEAD(&need_pages);
  449. for (i = p->bo_list->first_userptr;
  450. i < p->bo_list->num_entries; ++i) {
  451. e = &p->bo_list->array[i];
  452. if (amdgpu_ttm_tt_userptr_invalidated(e->robj->tbo.ttm,
  453. &e->user_invalidated) && e->user_pages) {
  454. /* We acquired a page array, but somebody
  455. * invalidated it. Free it an try again
  456. */
  457. release_pages(e->user_pages,
  458. e->robj->tbo.ttm->num_pages,
  459. false);
  460. drm_free_large(e->user_pages);
  461. e->user_pages = NULL;
  462. }
  463. if (e->robj->tbo.ttm->state != tt_bound &&
  464. !e->user_pages) {
  465. list_del(&e->tv.head);
  466. list_add(&e->tv.head, &need_pages);
  467. amdgpu_bo_unreserve(e->robj);
  468. }
  469. }
  470. if (list_empty(&need_pages))
  471. break;
  472. /* Unreserve everything again. */
  473. ttm_eu_backoff_reservation(&p->ticket, &p->validated);
  474. /* We tried too many times, just abort */
  475. if (!--tries) {
  476. r = -EDEADLK;
  477. DRM_ERROR("deadlock in %s\n", __func__);
  478. goto error_free_pages;
  479. }
  480. /* Fill the page arrays for all useptrs. */
  481. list_for_each_entry(e, &need_pages, tv.head) {
  482. struct ttm_tt *ttm = e->robj->tbo.ttm;
  483. e->user_pages = drm_calloc_large(ttm->num_pages,
  484. sizeof(struct page*));
  485. if (!e->user_pages) {
  486. r = -ENOMEM;
  487. DRM_ERROR("calloc failure in %s\n", __func__);
  488. goto error_free_pages;
  489. }
  490. r = amdgpu_ttm_tt_get_user_pages(ttm, e->user_pages);
  491. if (r) {
  492. DRM_ERROR("amdgpu_ttm_tt_get_user_pages failed.\n");
  493. drm_free_large(e->user_pages);
  494. e->user_pages = NULL;
  495. goto error_free_pages;
  496. }
  497. }
  498. /* And try again. */
  499. list_splice(&need_pages, &p->validated);
  500. }
  501. amdgpu_vm_get_pt_bos(p->adev, &fpriv->vm, &duplicates);
  502. p->bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(p->adev);
  503. p->bytes_moved = 0;
  504. p->evictable = list_last_entry(&p->validated,
  505. struct amdgpu_bo_list_entry,
  506. tv.head);
  507. r = amdgpu_cs_list_validate(p, &duplicates);
  508. if (r) {
  509. DRM_ERROR("amdgpu_cs_list_validate(duplicates) failed.\n");
  510. goto error_validate;
  511. }
  512. r = amdgpu_cs_list_validate(p, &p->validated);
  513. if (r) {
  514. DRM_ERROR("amdgpu_cs_list_validate(validated) failed.\n");
  515. goto error_validate;
  516. }
  517. amdgpu_cs_report_moved_bytes(p->adev, p->bytes_moved);
  518. fpriv->vm.last_eviction_counter =
  519. atomic64_read(&p->adev->num_evictions);
  520. if (p->bo_list) {
  521. struct amdgpu_bo *gds = p->bo_list->gds_obj;
  522. struct amdgpu_bo *gws = p->bo_list->gws_obj;
  523. struct amdgpu_bo *oa = p->bo_list->oa_obj;
  524. struct amdgpu_vm *vm = &fpriv->vm;
  525. unsigned i;
  526. for (i = 0; i < p->bo_list->num_entries; i++) {
  527. struct amdgpu_bo *bo = p->bo_list->array[i].robj;
  528. p->bo_list->array[i].bo_va = amdgpu_vm_bo_find(vm, bo);
  529. }
  530. if (gds) {
  531. p->job->gds_base = amdgpu_bo_gpu_offset(gds);
  532. p->job->gds_size = amdgpu_bo_size(gds);
  533. }
  534. if (gws) {
  535. p->job->gws_base = amdgpu_bo_gpu_offset(gws);
  536. p->job->gws_size = amdgpu_bo_size(gws);
  537. }
  538. if (oa) {
  539. p->job->oa_base = amdgpu_bo_gpu_offset(oa);
  540. p->job->oa_size = amdgpu_bo_size(oa);
  541. }
  542. }
  543. if (!r && p->uf_entry.robj) {
  544. struct amdgpu_bo *uf = p->uf_entry.robj;
  545. r = amdgpu_ttm_bind(&uf->tbo, &uf->tbo.mem);
  546. p->job->uf_addr += amdgpu_bo_gpu_offset(uf);
  547. }
  548. error_validate:
  549. if (r) {
  550. amdgpu_vm_move_pt_bos_in_lru(p->adev, &fpriv->vm);
  551. ttm_eu_backoff_reservation(&p->ticket, &p->validated);
  552. }
  553. error_free_pages:
  554. if (need_mmap_lock)
  555. up_read(&current->mm->mmap_sem);
  556. if (p->bo_list) {
  557. for (i = p->bo_list->first_userptr;
  558. i < p->bo_list->num_entries; ++i) {
  559. e = &p->bo_list->array[i];
  560. if (!e->user_pages)
  561. continue;
  562. release_pages(e->user_pages,
  563. e->robj->tbo.ttm->num_pages,
  564. false);
  565. drm_free_large(e->user_pages);
  566. }
  567. }
  568. return r;
  569. }
  570. static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
  571. {
  572. struct amdgpu_bo_list_entry *e;
  573. int r;
  574. list_for_each_entry(e, &p->validated, tv.head) {
  575. struct reservation_object *resv = e->robj->tbo.resv;
  576. r = amdgpu_sync_resv(p->adev, &p->job->sync, resv, p->filp);
  577. if (r)
  578. return r;
  579. }
  580. return 0;
  581. }
  582. /**
  583. * cs_parser_fini() - clean parser states
  584. * @parser: parser structure holding parsing context.
  585. * @error: error number
  586. *
  587. * If error is set than unvalidate buffer, otherwise just free memory
  588. * used by parsing context.
  589. **/
  590. static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
  591. {
  592. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  593. unsigned i;
  594. if (!error) {
  595. amdgpu_vm_move_pt_bos_in_lru(parser->adev, &fpriv->vm);
  596. ttm_eu_fence_buffer_objects(&parser->ticket,
  597. &parser->validated,
  598. parser->fence);
  599. } else if (backoff) {
  600. ttm_eu_backoff_reservation(&parser->ticket,
  601. &parser->validated);
  602. }
  603. fence_put(parser->fence);
  604. if (parser->ctx)
  605. amdgpu_ctx_put(parser->ctx);
  606. if (parser->bo_list)
  607. amdgpu_bo_list_put(parser->bo_list);
  608. for (i = 0; i < parser->nchunks; i++)
  609. drm_free_large(parser->chunks[i].kdata);
  610. kfree(parser->chunks);
  611. if (parser->job)
  612. amdgpu_job_free(parser->job);
  613. amdgpu_bo_unref(&parser->uf_entry.robj);
  614. }
  615. static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p,
  616. struct amdgpu_vm *vm)
  617. {
  618. struct amdgpu_device *adev = p->adev;
  619. struct amdgpu_bo_va *bo_va;
  620. struct amdgpu_bo *bo;
  621. int i, r;
  622. r = amdgpu_vm_update_page_directory(adev, vm);
  623. if (r)
  624. return r;
  625. r = amdgpu_sync_fence(adev, &p->job->sync, vm->page_directory_fence);
  626. if (r)
  627. return r;
  628. r = amdgpu_vm_clear_freed(adev, vm);
  629. if (r)
  630. return r;
  631. if (p->bo_list) {
  632. for (i = 0; i < p->bo_list->num_entries; i++) {
  633. struct fence *f;
  634. /* ignore duplicates */
  635. bo = p->bo_list->array[i].robj;
  636. if (!bo)
  637. continue;
  638. bo_va = p->bo_list->array[i].bo_va;
  639. if (bo_va == NULL)
  640. continue;
  641. r = amdgpu_vm_bo_update(adev, bo_va, false);
  642. if (r)
  643. return r;
  644. f = bo_va->last_pt_update;
  645. r = amdgpu_sync_fence(adev, &p->job->sync, f);
  646. if (r)
  647. return r;
  648. }
  649. }
  650. r = amdgpu_vm_clear_invalids(adev, vm, &p->job->sync);
  651. if (amdgpu_vm_debug && p->bo_list) {
  652. /* Invalidate all BOs to test for userspace bugs */
  653. for (i = 0; i < p->bo_list->num_entries; i++) {
  654. /* ignore duplicates */
  655. bo = p->bo_list->array[i].robj;
  656. if (!bo)
  657. continue;
  658. amdgpu_vm_bo_invalidate(adev, bo);
  659. }
  660. }
  661. return r;
  662. }
  663. static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
  664. struct amdgpu_cs_parser *p)
  665. {
  666. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  667. struct amdgpu_vm *vm = &fpriv->vm;
  668. struct amdgpu_ring *ring = p->job->ring;
  669. int i, r;
  670. /* Only for UVD/VCE VM emulation */
  671. if (ring->funcs->parse_cs) {
  672. p->job->vm = NULL;
  673. for (i = 0; i < p->job->num_ibs; i++) {
  674. r = amdgpu_ring_parse_cs(ring, p, i);
  675. if (r)
  676. return r;
  677. }
  678. } else {
  679. p->job->vm_pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
  680. r = amdgpu_bo_vm_update_pte(p, vm);
  681. if (r)
  682. return r;
  683. }
  684. return amdgpu_cs_sync_rings(p);
  685. }
  686. static int amdgpu_cs_handle_lockup(struct amdgpu_device *adev, int r)
  687. {
  688. if (r == -EDEADLK) {
  689. r = amdgpu_gpu_reset(adev);
  690. if (!r)
  691. r = -EAGAIN;
  692. }
  693. return r;
  694. }
  695. static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
  696. struct amdgpu_cs_parser *parser)
  697. {
  698. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  699. struct amdgpu_vm *vm = &fpriv->vm;
  700. int i, j;
  701. int r;
  702. for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) {
  703. struct amdgpu_cs_chunk *chunk;
  704. struct amdgpu_ib *ib;
  705. struct drm_amdgpu_cs_chunk_ib *chunk_ib;
  706. struct amdgpu_ring *ring;
  707. chunk = &parser->chunks[i];
  708. ib = &parser->job->ibs[j];
  709. chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
  710. if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
  711. continue;
  712. r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
  713. chunk_ib->ip_instance, chunk_ib->ring,
  714. &ring);
  715. if (r)
  716. return r;
  717. if (ib->flags & AMDGPU_IB_FLAG_PREAMBLE) {
  718. parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT;
  719. if (!parser->ctx->preamble_presented) {
  720. parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT_FIRST;
  721. parser->ctx->preamble_presented = true;
  722. }
  723. }
  724. if (parser->job->ring && parser->job->ring != ring)
  725. return -EINVAL;
  726. parser->job->ring = ring;
  727. if (ring->funcs->parse_cs) {
  728. struct amdgpu_bo_va_mapping *m;
  729. struct amdgpu_bo *aobj = NULL;
  730. uint64_t offset;
  731. uint8_t *kptr;
  732. m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
  733. &aobj);
  734. if (!aobj) {
  735. DRM_ERROR("IB va_start is invalid\n");
  736. return -EINVAL;
  737. }
  738. if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
  739. (m->it.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 = ((uint64_t)m->it.start) * AMDGPU_GPU_PAGE_SIZE;
  749. kptr += chunk_ib->va_start - offset;
  750. r = amdgpu_ib_get(adev, NULL, chunk_ib->ib_bytes, ib);
  751. if (r) {
  752. DRM_ERROR("Failed to get ib !\n");
  753. return r;
  754. }
  755. memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
  756. amdgpu_bo_kunmap(aobj);
  757. } else {
  758. r = amdgpu_ib_get(adev, vm, 0, ib);
  759. if (r) {
  760. DRM_ERROR("Failed to get ib !\n");
  761. return r;
  762. }
  763. ib->gpu_addr = chunk_ib->va_start;
  764. }
  765. ib->length_dw = chunk_ib->ib_bytes / 4;
  766. ib->flags = chunk_ib->flags;
  767. j++;
  768. }
  769. /* UVD & VCE fw doesn't support user fences */
  770. if (parser->job->uf_addr && (
  771. parser->job->ring->type == AMDGPU_RING_TYPE_UVD ||
  772. parser->job->ring->type == AMDGPU_RING_TYPE_VCE))
  773. return -EINVAL;
  774. return 0;
  775. }
  776. static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
  777. struct amdgpu_cs_parser *p)
  778. {
  779. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  780. int i, j, r;
  781. for (i = 0; i < p->nchunks; ++i) {
  782. struct drm_amdgpu_cs_chunk_dep *deps;
  783. struct amdgpu_cs_chunk *chunk;
  784. unsigned num_deps;
  785. chunk = &p->chunks[i];
  786. if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES)
  787. continue;
  788. deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
  789. num_deps = chunk->length_dw * 4 /
  790. sizeof(struct drm_amdgpu_cs_chunk_dep);
  791. for (j = 0; j < num_deps; ++j) {
  792. struct amdgpu_ring *ring;
  793. struct amdgpu_ctx *ctx;
  794. struct fence *fence;
  795. r = amdgpu_cs_get_ring(adev, deps[j].ip_type,
  796. deps[j].ip_instance,
  797. deps[j].ring, &ring);
  798. if (r)
  799. return r;
  800. ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id);
  801. if (ctx == NULL)
  802. return -EINVAL;
  803. fence = amdgpu_ctx_get_fence(ctx, ring,
  804. deps[j].handle);
  805. if (IS_ERR(fence)) {
  806. r = PTR_ERR(fence);
  807. amdgpu_ctx_put(ctx);
  808. return r;
  809. } else if (fence) {
  810. r = amdgpu_sync_fence(adev, &p->job->sync,
  811. fence);
  812. fence_put(fence);
  813. amdgpu_ctx_put(ctx);
  814. if (r)
  815. return r;
  816. }
  817. }
  818. }
  819. return 0;
  820. }
  821. static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
  822. union drm_amdgpu_cs *cs)
  823. {
  824. struct amdgpu_ring *ring = p->job->ring;
  825. struct amd_sched_entity *entity = &p->ctx->rings[ring->idx].entity;
  826. struct amdgpu_job *job;
  827. int r;
  828. job = p->job;
  829. p->job = NULL;
  830. r = amd_sched_job_init(&job->base, &ring->sched, entity, p->filp);
  831. if (r) {
  832. amdgpu_job_free(job);
  833. return r;
  834. }
  835. job->owner = p->filp;
  836. job->fence_ctx = entity->fence_context;
  837. p->fence = fence_get(&job->base.s_fence->finished);
  838. cs->out.handle = amdgpu_ctx_add_fence(p->ctx, ring, p->fence);
  839. job->uf_sequence = cs->out.handle;
  840. amdgpu_job_free_resources(job);
  841. trace_amdgpu_cs_ioctl(job);
  842. amd_sched_entity_push_job(&job->base);
  843. return 0;
  844. }
  845. int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  846. {
  847. struct amdgpu_device *adev = dev->dev_private;
  848. union drm_amdgpu_cs *cs = data;
  849. struct amdgpu_cs_parser parser = {};
  850. bool reserved_buffers = false;
  851. int i, r;
  852. if (!adev->accel_working)
  853. return -EBUSY;
  854. parser.adev = adev;
  855. parser.filp = filp;
  856. r = amdgpu_cs_parser_init(&parser, data);
  857. if (r) {
  858. DRM_ERROR("Failed to initialize parser !\n");
  859. amdgpu_cs_parser_fini(&parser, r, false);
  860. r = amdgpu_cs_handle_lockup(adev, r);
  861. return r;
  862. }
  863. r = amdgpu_cs_parser_bos(&parser, data);
  864. if (r == -ENOMEM)
  865. DRM_ERROR("Not enough memory for command submission!\n");
  866. else if (r && r != -ERESTARTSYS)
  867. DRM_ERROR("Failed to process the buffer list %d!\n", r);
  868. else if (!r) {
  869. reserved_buffers = true;
  870. r = amdgpu_cs_ib_fill(adev, &parser);
  871. }
  872. if (!r) {
  873. r = amdgpu_cs_dependencies(adev, &parser);
  874. if (r)
  875. DRM_ERROR("Failed in the dependencies handling %d!\n", r);
  876. }
  877. if (r)
  878. goto out;
  879. for (i = 0; i < parser.job->num_ibs; i++)
  880. trace_amdgpu_cs(&parser, i);
  881. r = amdgpu_cs_ib_vm_chunk(adev, &parser);
  882. if (r)
  883. goto out;
  884. r = amdgpu_cs_submit(&parser, cs);
  885. out:
  886. amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
  887. r = amdgpu_cs_handle_lockup(adev, r);
  888. return r;
  889. }
  890. /**
  891. * amdgpu_cs_wait_ioctl - wait for a command submission to finish
  892. *
  893. * @dev: drm device
  894. * @data: data from userspace
  895. * @filp: file private
  896. *
  897. * Wait for the command submission identified by handle to finish.
  898. */
  899. int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
  900. struct drm_file *filp)
  901. {
  902. union drm_amdgpu_wait_cs *wait = data;
  903. struct amdgpu_device *adev = dev->dev_private;
  904. unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
  905. struct amdgpu_ring *ring = NULL;
  906. struct amdgpu_ctx *ctx;
  907. struct fence *fence;
  908. long r;
  909. r = amdgpu_cs_get_ring(adev, wait->in.ip_type, wait->in.ip_instance,
  910. wait->in.ring, &ring);
  911. if (r)
  912. return r;
  913. ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
  914. if (ctx == NULL)
  915. return -EINVAL;
  916. fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
  917. if (IS_ERR(fence))
  918. r = PTR_ERR(fence);
  919. else if (fence) {
  920. r = fence_wait_timeout(fence, true, timeout);
  921. fence_put(fence);
  922. } else
  923. r = 1;
  924. amdgpu_ctx_put(ctx);
  925. if (r < 0)
  926. return r;
  927. memset(wait, 0, sizeof(*wait));
  928. wait->out.status = (r == 0);
  929. return 0;
  930. }
  931. /**
  932. * amdgpu_cs_find_bo_va - find bo_va for VM address
  933. *
  934. * @parser: command submission parser context
  935. * @addr: VM address
  936. * @bo: resulting BO of the mapping found
  937. *
  938. * Search the buffer objects in the command submission context for a certain
  939. * virtual memory address. Returns allocation structure when found, NULL
  940. * otherwise.
  941. */
  942. struct amdgpu_bo_va_mapping *
  943. amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
  944. uint64_t addr, struct amdgpu_bo **bo)
  945. {
  946. struct amdgpu_bo_va_mapping *mapping;
  947. unsigned i;
  948. if (!parser->bo_list)
  949. return NULL;
  950. addr /= AMDGPU_GPU_PAGE_SIZE;
  951. for (i = 0; i < parser->bo_list->num_entries; i++) {
  952. struct amdgpu_bo_list_entry *lobj;
  953. lobj = &parser->bo_list->array[i];
  954. if (!lobj->bo_va)
  955. continue;
  956. list_for_each_entry(mapping, &lobj->bo_va->valids, list) {
  957. if (mapping->it.start > addr ||
  958. addr > mapping->it.last)
  959. continue;
  960. *bo = lobj->bo_va->bo;
  961. return mapping;
  962. }
  963. list_for_each_entry(mapping, &lobj->bo_va->invalids, list) {
  964. if (mapping->it.start > addr ||
  965. addr > mapping->it.last)
  966. continue;
  967. *bo = lobj->bo_va->bo;
  968. return mapping;
  969. }
  970. }
  971. return NULL;
  972. }
  973. /**
  974. * amdgpu_cs_sysvm_access_required - make BOs accessible by the system VM
  975. *
  976. * @parser: command submission parser context
  977. *
  978. * Helper for UVD/VCE VM emulation, make sure BOs are accessible by the system VM.
  979. */
  980. int amdgpu_cs_sysvm_access_required(struct amdgpu_cs_parser *parser)
  981. {
  982. unsigned i;
  983. int r;
  984. if (!parser->bo_list)
  985. return 0;
  986. for (i = 0; i < parser->bo_list->num_entries; i++) {
  987. struct amdgpu_bo *bo = parser->bo_list->array[i].robj;
  988. r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem);
  989. if (unlikely(r))
  990. return r;
  991. if (bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)
  992. continue;
  993. bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
  994. amdgpu_ttm_placement_from_domain(bo, bo->allowed_domains);
  995. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
  996. if (unlikely(r))
  997. return r;
  998. }
  999. return 0;
  1000. }