amdgpu_cs.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  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 *validated)
  339. {
  340. uint32_t domain = validated->allowed_domains;
  341. int r;
  342. if (!p->evictable)
  343. return false;
  344. for (;&p->evictable->tv.head != &p->validated;
  345. p->evictable = list_prev_entry(p->evictable, tv.head)) {
  346. struct amdgpu_bo_list_entry *candidate = p->evictable;
  347. struct amdgpu_bo *bo = candidate->robj;
  348. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  349. u64 initial_bytes_moved;
  350. uint32_t other;
  351. /* If we reached our current BO we can forget it */
  352. if (candidate->robj == validated)
  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_validate(void *param, struct amdgpu_bo *bo)
  377. {
  378. struct amdgpu_cs_parser *p = param;
  379. int r;
  380. do {
  381. r = amdgpu_cs_bo_validate(p, bo);
  382. } while (r == -ENOMEM && amdgpu_cs_try_evict(p, bo));
  383. if (r)
  384. return r;
  385. if (bo->shadow)
  386. r = amdgpu_cs_bo_validate(p, bo);
  387. return r;
  388. }
  389. static int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p,
  390. struct list_head *validated)
  391. {
  392. struct amdgpu_bo_list_entry *lobj;
  393. int r;
  394. list_for_each_entry(lobj, validated, tv.head) {
  395. struct amdgpu_bo *bo = lobj->robj;
  396. bool binding_userptr = false;
  397. struct mm_struct *usermm;
  398. usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
  399. if (usermm && usermm != current->mm)
  400. return -EPERM;
  401. /* Check if we have user pages and nobody bound the BO already */
  402. if (lobj->user_pages && bo->tbo.ttm->state != tt_bound) {
  403. size_t size = sizeof(struct page *);
  404. size *= bo->tbo.ttm->num_pages;
  405. memcpy(bo->tbo.ttm->pages, lobj->user_pages, size);
  406. binding_userptr = true;
  407. }
  408. if (p->evictable == lobj)
  409. p->evictable = NULL;
  410. r = amdgpu_cs_validate(p, bo);
  411. if (r)
  412. return r;
  413. if (binding_userptr) {
  414. drm_free_large(lobj->user_pages);
  415. lobj->user_pages = NULL;
  416. }
  417. }
  418. return 0;
  419. }
  420. static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
  421. union drm_amdgpu_cs *cs)
  422. {
  423. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  424. struct amdgpu_bo_list_entry *e;
  425. struct list_head duplicates;
  426. bool need_mmap_lock = false;
  427. unsigned i, tries = 10;
  428. int r;
  429. INIT_LIST_HEAD(&p->validated);
  430. p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
  431. if (p->bo_list) {
  432. need_mmap_lock = p->bo_list->first_userptr !=
  433. p->bo_list->num_entries;
  434. amdgpu_bo_list_get_list(p->bo_list, &p->validated);
  435. }
  436. INIT_LIST_HEAD(&duplicates);
  437. amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
  438. if (p->uf_entry.robj)
  439. list_add(&p->uf_entry.tv.head, &p->validated);
  440. if (need_mmap_lock)
  441. down_read(&current->mm->mmap_sem);
  442. while (1) {
  443. struct list_head need_pages;
  444. unsigned i;
  445. r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
  446. &duplicates);
  447. if (unlikely(r != 0)) {
  448. DRM_ERROR("ttm_eu_reserve_buffers failed.\n");
  449. goto error_free_pages;
  450. }
  451. /* Without a BO list we don't have userptr BOs */
  452. if (!p->bo_list)
  453. break;
  454. INIT_LIST_HEAD(&need_pages);
  455. for (i = p->bo_list->first_userptr;
  456. i < p->bo_list->num_entries; ++i) {
  457. e = &p->bo_list->array[i];
  458. if (amdgpu_ttm_tt_userptr_invalidated(e->robj->tbo.ttm,
  459. &e->user_invalidated) && e->user_pages) {
  460. /* We acquired a page array, but somebody
  461. * invalidated it. Free it an try again
  462. */
  463. release_pages(e->user_pages,
  464. e->robj->tbo.ttm->num_pages,
  465. false);
  466. drm_free_large(e->user_pages);
  467. e->user_pages = NULL;
  468. }
  469. if (e->robj->tbo.ttm->state != tt_bound &&
  470. !e->user_pages) {
  471. list_del(&e->tv.head);
  472. list_add(&e->tv.head, &need_pages);
  473. amdgpu_bo_unreserve(e->robj);
  474. }
  475. }
  476. if (list_empty(&need_pages))
  477. break;
  478. /* Unreserve everything again. */
  479. ttm_eu_backoff_reservation(&p->ticket, &p->validated);
  480. /* We tried too many times, just abort */
  481. if (!--tries) {
  482. r = -EDEADLK;
  483. DRM_ERROR("deadlock in %s\n", __func__);
  484. goto error_free_pages;
  485. }
  486. /* Fill the page arrays for all useptrs. */
  487. list_for_each_entry(e, &need_pages, tv.head) {
  488. struct ttm_tt *ttm = e->robj->tbo.ttm;
  489. e->user_pages = drm_calloc_large(ttm->num_pages,
  490. sizeof(struct page*));
  491. if (!e->user_pages) {
  492. r = -ENOMEM;
  493. DRM_ERROR("calloc failure in %s\n", __func__);
  494. goto error_free_pages;
  495. }
  496. r = amdgpu_ttm_tt_get_user_pages(ttm, e->user_pages);
  497. if (r) {
  498. DRM_ERROR("amdgpu_ttm_tt_get_user_pages failed.\n");
  499. drm_free_large(e->user_pages);
  500. e->user_pages = NULL;
  501. goto error_free_pages;
  502. }
  503. }
  504. /* And try again. */
  505. list_splice(&need_pages, &p->validated);
  506. }
  507. p->bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(p->adev);
  508. p->bytes_moved = 0;
  509. p->evictable = list_last_entry(&p->validated,
  510. struct amdgpu_bo_list_entry,
  511. tv.head);
  512. r = amdgpu_vm_validate_pt_bos(p->adev, &fpriv->vm,
  513. amdgpu_cs_validate, p);
  514. if (r) {
  515. DRM_ERROR("amdgpu_vm_validate_pt_bos() failed.\n");
  516. goto error_validate;
  517. }
  518. r = amdgpu_cs_list_validate(p, &duplicates);
  519. if (r) {
  520. DRM_ERROR("amdgpu_cs_list_validate(duplicates) failed.\n");
  521. goto error_validate;
  522. }
  523. r = amdgpu_cs_list_validate(p, &p->validated);
  524. if (r) {
  525. DRM_ERROR("amdgpu_cs_list_validate(validated) failed.\n");
  526. goto error_validate;
  527. }
  528. amdgpu_cs_report_moved_bytes(p->adev, p->bytes_moved);
  529. fpriv->vm.last_eviction_counter =
  530. atomic64_read(&p->adev->num_evictions);
  531. if (p->bo_list) {
  532. struct amdgpu_bo *gds = p->bo_list->gds_obj;
  533. struct amdgpu_bo *gws = p->bo_list->gws_obj;
  534. struct amdgpu_bo *oa = p->bo_list->oa_obj;
  535. struct amdgpu_vm *vm = &fpriv->vm;
  536. unsigned i;
  537. for (i = 0; i < p->bo_list->num_entries; i++) {
  538. struct amdgpu_bo *bo = p->bo_list->array[i].robj;
  539. p->bo_list->array[i].bo_va = amdgpu_vm_bo_find(vm, bo);
  540. }
  541. if (gds) {
  542. p->job->gds_base = amdgpu_bo_gpu_offset(gds);
  543. p->job->gds_size = amdgpu_bo_size(gds);
  544. }
  545. if (gws) {
  546. p->job->gws_base = amdgpu_bo_gpu_offset(gws);
  547. p->job->gws_size = amdgpu_bo_size(gws);
  548. }
  549. if (oa) {
  550. p->job->oa_base = amdgpu_bo_gpu_offset(oa);
  551. p->job->oa_size = amdgpu_bo_size(oa);
  552. }
  553. }
  554. if (!r && p->uf_entry.robj) {
  555. struct amdgpu_bo *uf = p->uf_entry.robj;
  556. r = amdgpu_ttm_bind(&uf->tbo, &uf->tbo.mem);
  557. p->job->uf_addr += amdgpu_bo_gpu_offset(uf);
  558. }
  559. error_validate:
  560. if (r) {
  561. amdgpu_vm_move_pt_bos_in_lru(p->adev, &fpriv->vm);
  562. ttm_eu_backoff_reservation(&p->ticket, &p->validated);
  563. }
  564. error_free_pages:
  565. if (need_mmap_lock)
  566. up_read(&current->mm->mmap_sem);
  567. if (p->bo_list) {
  568. for (i = p->bo_list->first_userptr;
  569. i < p->bo_list->num_entries; ++i) {
  570. e = &p->bo_list->array[i];
  571. if (!e->user_pages)
  572. continue;
  573. release_pages(e->user_pages,
  574. e->robj->tbo.ttm->num_pages,
  575. false);
  576. drm_free_large(e->user_pages);
  577. }
  578. }
  579. return r;
  580. }
  581. static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
  582. {
  583. struct amdgpu_bo_list_entry *e;
  584. int r;
  585. list_for_each_entry(e, &p->validated, tv.head) {
  586. struct reservation_object *resv = e->robj->tbo.resv;
  587. r = amdgpu_sync_resv(p->adev, &p->job->sync, resv, p->filp);
  588. if (r)
  589. return r;
  590. }
  591. return 0;
  592. }
  593. /**
  594. * cs_parser_fini() - clean parser states
  595. * @parser: parser structure holding parsing context.
  596. * @error: error number
  597. *
  598. * If error is set than unvalidate buffer, otherwise just free memory
  599. * used by parsing context.
  600. **/
  601. static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
  602. {
  603. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  604. unsigned i;
  605. if (!error) {
  606. amdgpu_vm_move_pt_bos_in_lru(parser->adev, &fpriv->vm);
  607. ttm_eu_fence_buffer_objects(&parser->ticket,
  608. &parser->validated,
  609. parser->fence);
  610. } else if (backoff) {
  611. ttm_eu_backoff_reservation(&parser->ticket,
  612. &parser->validated);
  613. }
  614. dma_fence_put(parser->fence);
  615. if (parser->ctx)
  616. amdgpu_ctx_put(parser->ctx);
  617. if (parser->bo_list)
  618. amdgpu_bo_list_put(parser->bo_list);
  619. for (i = 0; i < parser->nchunks; i++)
  620. drm_free_large(parser->chunks[i].kdata);
  621. kfree(parser->chunks);
  622. if (parser->job)
  623. amdgpu_job_free(parser->job);
  624. amdgpu_bo_unref(&parser->uf_entry.robj);
  625. }
  626. static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p,
  627. struct amdgpu_vm *vm)
  628. {
  629. struct amdgpu_device *adev = p->adev;
  630. struct amdgpu_bo_va *bo_va;
  631. struct amdgpu_bo *bo;
  632. int i, r;
  633. r = amdgpu_vm_update_page_directory(adev, vm);
  634. if (r)
  635. return r;
  636. r = amdgpu_sync_fence(adev, &p->job->sync, vm->page_directory_fence);
  637. if (r)
  638. return r;
  639. r = amdgpu_vm_clear_freed(adev, vm);
  640. if (r)
  641. return r;
  642. if (p->bo_list) {
  643. for (i = 0; i < p->bo_list->num_entries; i++) {
  644. struct dma_fence *f;
  645. /* ignore duplicates */
  646. bo = p->bo_list->array[i].robj;
  647. if (!bo)
  648. continue;
  649. bo_va = p->bo_list->array[i].bo_va;
  650. if (bo_va == NULL)
  651. continue;
  652. r = amdgpu_vm_bo_update(adev, bo_va, false);
  653. if (r)
  654. return r;
  655. f = bo_va->last_pt_update;
  656. r = amdgpu_sync_fence(adev, &p->job->sync, f);
  657. if (r)
  658. return r;
  659. }
  660. }
  661. r = amdgpu_vm_clear_invalids(adev, vm, &p->job->sync);
  662. if (amdgpu_vm_debug && p->bo_list) {
  663. /* Invalidate all BOs to test for userspace bugs */
  664. for (i = 0; i < p->bo_list->num_entries; i++) {
  665. /* ignore duplicates */
  666. bo = p->bo_list->array[i].robj;
  667. if (!bo)
  668. continue;
  669. amdgpu_vm_bo_invalidate(adev, bo);
  670. }
  671. }
  672. return r;
  673. }
  674. static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
  675. struct amdgpu_cs_parser *p)
  676. {
  677. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  678. struct amdgpu_vm *vm = &fpriv->vm;
  679. struct amdgpu_ring *ring = p->job->ring;
  680. int i, r;
  681. /* Only for UVD/VCE VM emulation */
  682. if (ring->funcs->parse_cs) {
  683. for (i = 0; i < p->job->num_ibs; i++) {
  684. r = amdgpu_ring_parse_cs(ring, p, i);
  685. if (r)
  686. return r;
  687. }
  688. }
  689. if (p->job->vm) {
  690. p->job->vm_pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
  691. r = amdgpu_bo_vm_update_pte(p, vm);
  692. if (r)
  693. return r;
  694. }
  695. return amdgpu_cs_sync_rings(p);
  696. }
  697. static int amdgpu_cs_handle_lockup(struct amdgpu_device *adev, int r)
  698. {
  699. if (r == -EDEADLK) {
  700. r = amdgpu_gpu_reset(adev);
  701. if (!r)
  702. r = -EAGAIN;
  703. }
  704. return r;
  705. }
  706. static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
  707. struct amdgpu_cs_parser *parser)
  708. {
  709. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  710. struct amdgpu_vm *vm = &fpriv->vm;
  711. int i, j;
  712. int r;
  713. for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) {
  714. struct amdgpu_cs_chunk *chunk;
  715. struct amdgpu_ib *ib;
  716. struct drm_amdgpu_cs_chunk_ib *chunk_ib;
  717. struct amdgpu_ring *ring;
  718. chunk = &parser->chunks[i];
  719. ib = &parser->job->ibs[j];
  720. chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
  721. if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
  722. continue;
  723. r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
  724. chunk_ib->ip_instance, chunk_ib->ring,
  725. &ring);
  726. if (r)
  727. return r;
  728. if (ib->flags & AMDGPU_IB_FLAG_PREAMBLE) {
  729. parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT;
  730. if (!parser->ctx->preamble_presented) {
  731. parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT_FIRST;
  732. parser->ctx->preamble_presented = true;
  733. }
  734. }
  735. if (parser->job->ring && parser->job->ring != ring)
  736. return -EINVAL;
  737. parser->job->ring = ring;
  738. if (ring->funcs->parse_cs) {
  739. struct amdgpu_bo_va_mapping *m;
  740. struct amdgpu_bo *aobj = NULL;
  741. uint64_t offset;
  742. uint8_t *kptr;
  743. m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
  744. &aobj);
  745. if (!aobj) {
  746. DRM_ERROR("IB va_start is invalid\n");
  747. return -EINVAL;
  748. }
  749. if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
  750. (m->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
  751. DRM_ERROR("IB va_start+ib_bytes is invalid\n");
  752. return -EINVAL;
  753. }
  754. /* the IB should be reserved at this point */
  755. r = amdgpu_bo_kmap(aobj, (void **)&kptr);
  756. if (r) {
  757. return r;
  758. }
  759. offset = ((uint64_t)m->it.start) * AMDGPU_GPU_PAGE_SIZE;
  760. kptr += chunk_ib->va_start - offset;
  761. r = amdgpu_ib_get(adev, vm, chunk_ib->ib_bytes, ib);
  762. if (r) {
  763. DRM_ERROR("Failed to get ib !\n");
  764. return r;
  765. }
  766. memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
  767. amdgpu_bo_kunmap(aobj);
  768. } else {
  769. r = amdgpu_ib_get(adev, vm, 0, ib);
  770. if (r) {
  771. DRM_ERROR("Failed to get ib !\n");
  772. return r;
  773. }
  774. }
  775. ib->gpu_addr = chunk_ib->va_start;
  776. ib->length_dw = chunk_ib->ib_bytes / 4;
  777. ib->flags = chunk_ib->flags;
  778. j++;
  779. }
  780. /* UVD & VCE fw doesn't support user fences */
  781. if (parser->job->uf_addr && (
  782. parser->job->ring->funcs->type == AMDGPU_RING_TYPE_UVD ||
  783. parser->job->ring->funcs->type == AMDGPU_RING_TYPE_VCE))
  784. return -EINVAL;
  785. return 0;
  786. }
  787. static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
  788. struct amdgpu_cs_parser *p)
  789. {
  790. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  791. int i, j, r;
  792. for (i = 0; i < p->nchunks; ++i) {
  793. struct drm_amdgpu_cs_chunk_dep *deps;
  794. struct amdgpu_cs_chunk *chunk;
  795. unsigned num_deps;
  796. chunk = &p->chunks[i];
  797. if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES)
  798. continue;
  799. deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
  800. num_deps = chunk->length_dw * 4 /
  801. sizeof(struct drm_amdgpu_cs_chunk_dep);
  802. for (j = 0; j < num_deps; ++j) {
  803. struct amdgpu_ring *ring;
  804. struct amdgpu_ctx *ctx;
  805. struct dma_fence *fence;
  806. r = amdgpu_cs_get_ring(adev, deps[j].ip_type,
  807. deps[j].ip_instance,
  808. deps[j].ring, &ring);
  809. if (r)
  810. return r;
  811. ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id);
  812. if (ctx == NULL)
  813. return -EINVAL;
  814. fence = amdgpu_ctx_get_fence(ctx, ring,
  815. deps[j].handle);
  816. if (IS_ERR(fence)) {
  817. r = PTR_ERR(fence);
  818. amdgpu_ctx_put(ctx);
  819. return r;
  820. } else if (fence) {
  821. r = amdgpu_sync_fence(adev, &p->job->sync,
  822. fence);
  823. dma_fence_put(fence);
  824. amdgpu_ctx_put(ctx);
  825. if (r)
  826. return r;
  827. }
  828. }
  829. }
  830. return 0;
  831. }
  832. static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
  833. union drm_amdgpu_cs *cs)
  834. {
  835. struct amdgpu_ring *ring = p->job->ring;
  836. struct amd_sched_entity *entity = &p->ctx->rings[ring->idx].entity;
  837. struct amdgpu_job *job;
  838. int r;
  839. job = p->job;
  840. p->job = NULL;
  841. r = amd_sched_job_init(&job->base, &ring->sched, entity, p->filp);
  842. if (r) {
  843. amdgpu_job_free(job);
  844. return r;
  845. }
  846. job->owner = p->filp;
  847. job->fence_ctx = entity->fence_context;
  848. p->fence = dma_fence_get(&job->base.s_fence->finished);
  849. cs->out.handle = amdgpu_ctx_add_fence(p->ctx, ring, p->fence);
  850. job->uf_sequence = cs->out.handle;
  851. amdgpu_job_free_resources(job);
  852. trace_amdgpu_cs_ioctl(job);
  853. amd_sched_entity_push_job(&job->base);
  854. return 0;
  855. }
  856. int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  857. {
  858. struct amdgpu_device *adev = dev->dev_private;
  859. union drm_amdgpu_cs *cs = data;
  860. struct amdgpu_cs_parser parser = {};
  861. bool reserved_buffers = false;
  862. int i, r;
  863. if (!adev->accel_working)
  864. return -EBUSY;
  865. parser.adev = adev;
  866. parser.filp = filp;
  867. r = amdgpu_cs_parser_init(&parser, data);
  868. if (r) {
  869. DRM_ERROR("Failed to initialize parser !\n");
  870. amdgpu_cs_parser_fini(&parser, r, false);
  871. r = amdgpu_cs_handle_lockup(adev, r);
  872. return r;
  873. }
  874. r = amdgpu_cs_parser_bos(&parser, data);
  875. if (r == -ENOMEM)
  876. DRM_ERROR("Not enough memory for command submission!\n");
  877. else if (r && r != -ERESTARTSYS)
  878. DRM_ERROR("Failed to process the buffer list %d!\n", r);
  879. else if (!r) {
  880. reserved_buffers = true;
  881. r = amdgpu_cs_ib_fill(adev, &parser);
  882. }
  883. if (!r) {
  884. r = amdgpu_cs_dependencies(adev, &parser);
  885. if (r)
  886. DRM_ERROR("Failed in the dependencies handling %d!\n", r);
  887. }
  888. if (r)
  889. goto out;
  890. for (i = 0; i < parser.job->num_ibs; i++)
  891. trace_amdgpu_cs(&parser, i);
  892. r = amdgpu_cs_ib_vm_chunk(adev, &parser);
  893. if (r)
  894. goto out;
  895. r = amdgpu_cs_submit(&parser, cs);
  896. out:
  897. amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
  898. r = amdgpu_cs_handle_lockup(adev, r);
  899. return r;
  900. }
  901. /**
  902. * amdgpu_cs_wait_ioctl - wait for a command submission to finish
  903. *
  904. * @dev: drm device
  905. * @data: data from userspace
  906. * @filp: file private
  907. *
  908. * Wait for the command submission identified by handle to finish.
  909. */
  910. int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
  911. struct drm_file *filp)
  912. {
  913. union drm_amdgpu_wait_cs *wait = data;
  914. struct amdgpu_device *adev = dev->dev_private;
  915. unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
  916. struct amdgpu_ring *ring = NULL;
  917. struct amdgpu_ctx *ctx;
  918. struct dma_fence *fence;
  919. long r;
  920. r = amdgpu_cs_get_ring(adev, wait->in.ip_type, wait->in.ip_instance,
  921. wait->in.ring, &ring);
  922. if (r)
  923. return r;
  924. ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
  925. if (ctx == NULL)
  926. return -EINVAL;
  927. fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
  928. if (IS_ERR(fence))
  929. r = PTR_ERR(fence);
  930. else if (fence) {
  931. r = dma_fence_wait_timeout(fence, true, timeout);
  932. dma_fence_put(fence);
  933. } else
  934. r = 1;
  935. amdgpu_ctx_put(ctx);
  936. if (r < 0)
  937. return r;
  938. memset(wait, 0, sizeof(*wait));
  939. wait->out.status = (r == 0);
  940. return 0;
  941. }
  942. /**
  943. * amdgpu_cs_find_bo_va - find bo_va for VM address
  944. *
  945. * @parser: command submission parser context
  946. * @addr: VM address
  947. * @bo: resulting BO of the mapping found
  948. *
  949. * Search the buffer objects in the command submission context for a certain
  950. * virtual memory address. Returns allocation structure when found, NULL
  951. * otherwise.
  952. */
  953. struct amdgpu_bo_va_mapping *
  954. amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
  955. uint64_t addr, struct amdgpu_bo **bo)
  956. {
  957. struct amdgpu_bo_va_mapping *mapping;
  958. unsigned i;
  959. if (!parser->bo_list)
  960. return NULL;
  961. addr /= AMDGPU_GPU_PAGE_SIZE;
  962. for (i = 0; i < parser->bo_list->num_entries; i++) {
  963. struct amdgpu_bo_list_entry *lobj;
  964. lobj = &parser->bo_list->array[i];
  965. if (!lobj->bo_va)
  966. continue;
  967. list_for_each_entry(mapping, &lobj->bo_va->valids, list) {
  968. if (mapping->it.start > addr ||
  969. addr > mapping->it.last)
  970. continue;
  971. *bo = lobj->bo_va->bo;
  972. return mapping;
  973. }
  974. list_for_each_entry(mapping, &lobj->bo_va->invalids, list) {
  975. if (mapping->it.start > addr ||
  976. addr > mapping->it.last)
  977. continue;
  978. *bo = lobj->bo_va->bo;
  979. return mapping;
  980. }
  981. }
  982. return NULL;
  983. }
  984. /**
  985. * amdgpu_cs_sysvm_access_required - make BOs accessible by the system VM
  986. *
  987. * @parser: command submission parser context
  988. *
  989. * Helper for UVD/VCE VM emulation, make sure BOs are accessible by the system VM.
  990. */
  991. int amdgpu_cs_sysvm_access_required(struct amdgpu_cs_parser *parser)
  992. {
  993. unsigned i;
  994. int r;
  995. if (!parser->bo_list)
  996. return 0;
  997. for (i = 0; i < parser->bo_list->num_entries; i++) {
  998. struct amdgpu_bo *bo = parser->bo_list->array[i].robj;
  999. r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem);
  1000. if (unlikely(r))
  1001. return r;
  1002. if (bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)
  1003. continue;
  1004. bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
  1005. amdgpu_ttm_placement_from_domain(bo, bo->allowed_domains);
  1006. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
  1007. if (unlikely(r))
  1008. return r;
  1009. }
  1010. return 0;
  1011. }