amdgpu_cs.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391
  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->shadow);
  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. if (r != -ERESTARTSYS)
  449. DRM_ERROR("ttm_eu_reserve_buffers failed.\n");
  450. goto error_free_pages;
  451. }
  452. /* Without a BO list we don't have userptr BOs */
  453. if (!p->bo_list)
  454. break;
  455. INIT_LIST_HEAD(&need_pages);
  456. for (i = p->bo_list->first_userptr;
  457. i < p->bo_list->num_entries; ++i) {
  458. e = &p->bo_list->array[i];
  459. if (amdgpu_ttm_tt_userptr_invalidated(e->robj->tbo.ttm,
  460. &e->user_invalidated) && e->user_pages) {
  461. /* We acquired a page array, but somebody
  462. * invalidated it. Free it an try again
  463. */
  464. release_pages(e->user_pages,
  465. e->robj->tbo.ttm->num_pages,
  466. false);
  467. drm_free_large(e->user_pages);
  468. e->user_pages = NULL;
  469. }
  470. if (e->robj->tbo.ttm->state != tt_bound &&
  471. !e->user_pages) {
  472. list_del(&e->tv.head);
  473. list_add(&e->tv.head, &need_pages);
  474. amdgpu_bo_unreserve(e->robj);
  475. }
  476. }
  477. if (list_empty(&need_pages))
  478. break;
  479. /* Unreserve everything again. */
  480. ttm_eu_backoff_reservation(&p->ticket, &p->validated);
  481. /* We tried too many times, just abort */
  482. if (!--tries) {
  483. r = -EDEADLK;
  484. DRM_ERROR("deadlock in %s\n", __func__);
  485. goto error_free_pages;
  486. }
  487. /* Fill the page arrays for all useptrs. */
  488. list_for_each_entry(e, &need_pages, tv.head) {
  489. struct ttm_tt *ttm = e->robj->tbo.ttm;
  490. e->user_pages = drm_calloc_large(ttm->num_pages,
  491. sizeof(struct page*));
  492. if (!e->user_pages) {
  493. r = -ENOMEM;
  494. DRM_ERROR("calloc failure in %s\n", __func__);
  495. goto error_free_pages;
  496. }
  497. r = amdgpu_ttm_tt_get_user_pages(ttm, e->user_pages);
  498. if (r) {
  499. DRM_ERROR("amdgpu_ttm_tt_get_user_pages failed.\n");
  500. drm_free_large(e->user_pages);
  501. e->user_pages = NULL;
  502. goto error_free_pages;
  503. }
  504. }
  505. /* And try again. */
  506. list_splice(&need_pages, &p->validated);
  507. }
  508. p->bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(p->adev);
  509. p->bytes_moved = 0;
  510. p->evictable = list_last_entry(&p->validated,
  511. struct amdgpu_bo_list_entry,
  512. tv.head);
  513. r = amdgpu_vm_validate_pt_bos(p->adev, &fpriv->vm,
  514. amdgpu_cs_validate, p);
  515. if (r) {
  516. DRM_ERROR("amdgpu_vm_validate_pt_bos() failed.\n");
  517. goto error_validate;
  518. }
  519. r = amdgpu_cs_list_validate(p, &duplicates);
  520. if (r) {
  521. DRM_ERROR("amdgpu_cs_list_validate(duplicates) failed.\n");
  522. goto error_validate;
  523. }
  524. r = amdgpu_cs_list_validate(p, &p->validated);
  525. if (r) {
  526. DRM_ERROR("amdgpu_cs_list_validate(validated) failed.\n");
  527. goto error_validate;
  528. }
  529. amdgpu_cs_report_moved_bytes(p->adev, p->bytes_moved);
  530. fpriv->vm.last_eviction_counter =
  531. atomic64_read(&p->adev->num_evictions);
  532. if (p->bo_list) {
  533. struct amdgpu_bo *gds = p->bo_list->gds_obj;
  534. struct amdgpu_bo *gws = p->bo_list->gws_obj;
  535. struct amdgpu_bo *oa = p->bo_list->oa_obj;
  536. struct amdgpu_vm *vm = &fpriv->vm;
  537. unsigned i;
  538. for (i = 0; i < p->bo_list->num_entries; i++) {
  539. struct amdgpu_bo *bo = p->bo_list->array[i].robj;
  540. p->bo_list->array[i].bo_va = amdgpu_vm_bo_find(vm, bo);
  541. }
  542. if (gds) {
  543. p->job->gds_base = amdgpu_bo_gpu_offset(gds);
  544. p->job->gds_size = amdgpu_bo_size(gds);
  545. }
  546. if (gws) {
  547. p->job->gws_base = amdgpu_bo_gpu_offset(gws);
  548. p->job->gws_size = amdgpu_bo_size(gws);
  549. }
  550. if (oa) {
  551. p->job->oa_base = amdgpu_bo_gpu_offset(oa);
  552. p->job->oa_size = amdgpu_bo_size(oa);
  553. }
  554. }
  555. if (!r && p->uf_entry.robj) {
  556. struct amdgpu_bo *uf = p->uf_entry.robj;
  557. r = amdgpu_ttm_bind(&uf->tbo, &uf->tbo.mem);
  558. p->job->uf_addr += amdgpu_bo_gpu_offset(uf);
  559. }
  560. error_validate:
  561. if (r) {
  562. amdgpu_vm_move_pt_bos_in_lru(p->adev, &fpriv->vm);
  563. ttm_eu_backoff_reservation(&p->ticket, &p->validated);
  564. }
  565. error_free_pages:
  566. if (need_mmap_lock)
  567. up_read(&current->mm->mmap_sem);
  568. if (p->bo_list) {
  569. for (i = p->bo_list->first_userptr;
  570. i < p->bo_list->num_entries; ++i) {
  571. e = &p->bo_list->array[i];
  572. if (!e->user_pages)
  573. continue;
  574. release_pages(e->user_pages,
  575. e->robj->tbo.ttm->num_pages,
  576. false);
  577. drm_free_large(e->user_pages);
  578. }
  579. }
  580. return r;
  581. }
  582. static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
  583. {
  584. struct amdgpu_bo_list_entry *e;
  585. int r;
  586. list_for_each_entry(e, &p->validated, tv.head) {
  587. struct reservation_object *resv = e->robj->tbo.resv;
  588. r = amdgpu_sync_resv(p->adev, &p->job->sync, resv, p->filp);
  589. if (r)
  590. return r;
  591. }
  592. return 0;
  593. }
  594. /**
  595. * cs_parser_fini() - clean parser states
  596. * @parser: parser structure holding parsing context.
  597. * @error: error number
  598. *
  599. * If error is set than unvalidate buffer, otherwise just free memory
  600. * used by parsing context.
  601. **/
  602. static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
  603. {
  604. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  605. unsigned i;
  606. if (!error) {
  607. amdgpu_vm_move_pt_bos_in_lru(parser->adev, &fpriv->vm);
  608. ttm_eu_fence_buffer_objects(&parser->ticket,
  609. &parser->validated,
  610. parser->fence);
  611. } else if (backoff) {
  612. ttm_eu_backoff_reservation(&parser->ticket,
  613. &parser->validated);
  614. }
  615. dma_fence_put(parser->fence);
  616. if (parser->ctx)
  617. amdgpu_ctx_put(parser->ctx);
  618. if (parser->bo_list)
  619. amdgpu_bo_list_put(parser->bo_list);
  620. for (i = 0; i < parser->nchunks; i++)
  621. drm_free_large(parser->chunks[i].kdata);
  622. kfree(parser->chunks);
  623. if (parser->job)
  624. amdgpu_job_free(parser->job);
  625. amdgpu_bo_unref(&parser->uf_entry.robj);
  626. }
  627. static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p,
  628. struct amdgpu_vm *vm)
  629. {
  630. struct amdgpu_device *adev = p->adev;
  631. struct amdgpu_bo_va *bo_va;
  632. struct amdgpu_bo *bo;
  633. int i, r;
  634. r = amdgpu_vm_update_page_directory(adev, vm);
  635. if (r)
  636. return r;
  637. r = amdgpu_sync_fence(adev, &p->job->sync, vm->page_directory_fence);
  638. if (r)
  639. return r;
  640. r = amdgpu_vm_clear_freed(adev, vm);
  641. if (r)
  642. return r;
  643. if (p->bo_list) {
  644. for (i = 0; i < p->bo_list->num_entries; i++) {
  645. struct dma_fence *f;
  646. /* ignore duplicates */
  647. bo = p->bo_list->array[i].robj;
  648. if (!bo)
  649. continue;
  650. bo_va = p->bo_list->array[i].bo_va;
  651. if (bo_va == NULL)
  652. continue;
  653. r = amdgpu_vm_bo_update(adev, bo_va, false);
  654. if (r)
  655. return r;
  656. f = bo_va->last_pt_update;
  657. r = amdgpu_sync_fence(adev, &p->job->sync, f);
  658. if (r)
  659. return r;
  660. }
  661. }
  662. r = amdgpu_vm_clear_invalids(adev, vm, &p->job->sync);
  663. if (amdgpu_vm_debug && p->bo_list) {
  664. /* Invalidate all BOs to test for userspace bugs */
  665. for (i = 0; i < p->bo_list->num_entries; i++) {
  666. /* ignore duplicates */
  667. bo = p->bo_list->array[i].robj;
  668. if (!bo)
  669. continue;
  670. amdgpu_vm_bo_invalidate(adev, bo);
  671. }
  672. }
  673. return r;
  674. }
  675. static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
  676. struct amdgpu_cs_parser *p)
  677. {
  678. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  679. struct amdgpu_vm *vm = &fpriv->vm;
  680. struct amdgpu_ring *ring = p->job->ring;
  681. int i, r;
  682. /* Only for UVD/VCE VM emulation */
  683. if (ring->funcs->parse_cs) {
  684. for (i = 0; i < p->job->num_ibs; i++) {
  685. r = amdgpu_ring_parse_cs(ring, p, i);
  686. if (r)
  687. return r;
  688. }
  689. }
  690. if (p->job->vm) {
  691. p->job->vm_pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
  692. r = amdgpu_bo_vm_update_pte(p, vm);
  693. if (r)
  694. return r;
  695. }
  696. return amdgpu_cs_sync_rings(p);
  697. }
  698. static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
  699. struct amdgpu_cs_parser *parser)
  700. {
  701. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  702. struct amdgpu_vm *vm = &fpriv->vm;
  703. int i, j;
  704. int r;
  705. for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) {
  706. struct amdgpu_cs_chunk *chunk;
  707. struct amdgpu_ib *ib;
  708. struct drm_amdgpu_cs_chunk_ib *chunk_ib;
  709. struct amdgpu_ring *ring;
  710. chunk = &parser->chunks[i];
  711. ib = &parser->job->ibs[j];
  712. chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
  713. if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
  714. continue;
  715. r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
  716. chunk_ib->ip_instance, chunk_ib->ring,
  717. &ring);
  718. if (r)
  719. return r;
  720. if (ib->flags & AMDGPU_IB_FLAG_PREAMBLE) {
  721. parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT;
  722. if (!parser->ctx->preamble_presented) {
  723. parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT_FIRST;
  724. parser->ctx->preamble_presented = true;
  725. }
  726. }
  727. if (parser->job->ring && parser->job->ring != ring)
  728. return -EINVAL;
  729. parser->job->ring = ring;
  730. if (ring->funcs->parse_cs) {
  731. struct amdgpu_bo_va_mapping *m;
  732. struct amdgpu_bo *aobj = NULL;
  733. uint64_t offset;
  734. uint8_t *kptr;
  735. m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
  736. &aobj);
  737. if (!aobj) {
  738. DRM_ERROR("IB va_start is invalid\n");
  739. return -EINVAL;
  740. }
  741. if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
  742. (m->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
  743. DRM_ERROR("IB va_start+ib_bytes is invalid\n");
  744. return -EINVAL;
  745. }
  746. /* the IB should be reserved at this point */
  747. r = amdgpu_bo_kmap(aobj, (void **)&kptr);
  748. if (r) {
  749. return r;
  750. }
  751. offset = ((uint64_t)m->it.start) * AMDGPU_GPU_PAGE_SIZE;
  752. kptr += chunk_ib->va_start - offset;
  753. r = amdgpu_ib_get(adev, vm, chunk_ib->ib_bytes, ib);
  754. if (r) {
  755. DRM_ERROR("Failed to get ib !\n");
  756. return r;
  757. }
  758. memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
  759. amdgpu_bo_kunmap(aobj);
  760. } else {
  761. r = amdgpu_ib_get(adev, vm, 0, ib);
  762. if (r) {
  763. DRM_ERROR("Failed to get ib !\n");
  764. return r;
  765. }
  766. }
  767. ib->gpu_addr = chunk_ib->va_start;
  768. ib->length_dw = chunk_ib->ib_bytes / 4;
  769. ib->flags = chunk_ib->flags;
  770. j++;
  771. }
  772. /* UVD & VCE fw doesn't support user fences */
  773. if (parser->job->uf_addr && (
  774. parser->job->ring->funcs->type == AMDGPU_RING_TYPE_UVD ||
  775. parser->job->ring->funcs->type == AMDGPU_RING_TYPE_VCE))
  776. return -EINVAL;
  777. return 0;
  778. }
  779. static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
  780. struct amdgpu_cs_parser *p)
  781. {
  782. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  783. int i, j, r;
  784. for (i = 0; i < p->nchunks; ++i) {
  785. struct drm_amdgpu_cs_chunk_dep *deps;
  786. struct amdgpu_cs_chunk *chunk;
  787. unsigned num_deps;
  788. chunk = &p->chunks[i];
  789. if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES)
  790. continue;
  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 (j = 0; j < num_deps; ++j) {
  795. struct amdgpu_ring *ring;
  796. struct amdgpu_ctx *ctx;
  797. struct dma_fence *fence;
  798. r = amdgpu_cs_get_ring(adev, deps[j].ip_type,
  799. deps[j].ip_instance,
  800. deps[j].ring, &ring);
  801. if (r)
  802. return r;
  803. ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id);
  804. if (ctx == NULL)
  805. return -EINVAL;
  806. fence = amdgpu_ctx_get_fence(ctx, ring,
  807. deps[j].handle);
  808. if (IS_ERR(fence)) {
  809. r = PTR_ERR(fence);
  810. amdgpu_ctx_put(ctx);
  811. return r;
  812. } else if (fence) {
  813. r = amdgpu_sync_fence(adev, &p->job->sync,
  814. fence);
  815. dma_fence_put(fence);
  816. amdgpu_ctx_put(ctx);
  817. if (r)
  818. return r;
  819. }
  820. }
  821. }
  822. return 0;
  823. }
  824. static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
  825. union drm_amdgpu_cs *cs)
  826. {
  827. struct amdgpu_ring *ring = p->job->ring;
  828. struct amd_sched_entity *entity = &p->ctx->rings[ring->idx].entity;
  829. struct amdgpu_job *job;
  830. int r;
  831. job = p->job;
  832. p->job = NULL;
  833. r = amd_sched_job_init(&job->base, &ring->sched, entity, p->filp);
  834. if (r) {
  835. amdgpu_job_free(job);
  836. return r;
  837. }
  838. job->owner = p->filp;
  839. job->fence_ctx = entity->fence_context;
  840. p->fence = dma_fence_get(&job->base.s_fence->finished);
  841. cs->out.handle = amdgpu_ctx_add_fence(p->ctx, ring, p->fence);
  842. job->uf_sequence = cs->out.handle;
  843. amdgpu_job_free_resources(job);
  844. trace_amdgpu_cs_ioctl(job);
  845. amd_sched_entity_push_job(&job->base);
  846. return 0;
  847. }
  848. int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  849. {
  850. struct amdgpu_device *adev = dev->dev_private;
  851. union drm_amdgpu_cs *cs = data;
  852. struct amdgpu_cs_parser parser = {};
  853. bool reserved_buffers = false;
  854. int i, r;
  855. if (!adev->accel_working)
  856. return -EBUSY;
  857. parser.adev = adev;
  858. parser.filp = filp;
  859. r = amdgpu_cs_parser_init(&parser, data);
  860. if (r) {
  861. DRM_ERROR("Failed to initialize parser !\n");
  862. goto out;
  863. }
  864. r = amdgpu_cs_parser_bos(&parser, data);
  865. if (r) {
  866. if (r == -ENOMEM)
  867. DRM_ERROR("Not enough memory for command submission!\n");
  868. else if (r != -ERESTARTSYS)
  869. DRM_ERROR("Failed to process the buffer list %d!\n", r);
  870. goto out;
  871. }
  872. reserved_buffers = true;
  873. r = amdgpu_cs_ib_fill(adev, &parser);
  874. if (r)
  875. goto out;
  876. r = amdgpu_cs_dependencies(adev, &parser);
  877. if (r) {
  878. DRM_ERROR("Failed in the dependencies handling %d!\n", r);
  879. goto out;
  880. }
  881. for (i = 0; i < parser.job->num_ibs; i++)
  882. trace_amdgpu_cs(&parser, i);
  883. r = amdgpu_cs_ib_vm_chunk(adev, &parser);
  884. if (r)
  885. goto out;
  886. r = amdgpu_cs_submit(&parser, cs);
  887. out:
  888. amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
  889. return r;
  890. }
  891. /**
  892. * amdgpu_cs_wait_ioctl - wait for a command submission to finish
  893. *
  894. * @dev: drm device
  895. * @data: data from userspace
  896. * @filp: file private
  897. *
  898. * Wait for the command submission identified by handle to finish.
  899. */
  900. int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
  901. struct drm_file *filp)
  902. {
  903. union drm_amdgpu_wait_cs *wait = data;
  904. struct amdgpu_device *adev = dev->dev_private;
  905. unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
  906. struct amdgpu_ring *ring = NULL;
  907. struct amdgpu_ctx *ctx;
  908. struct dma_fence *fence;
  909. long r;
  910. r = amdgpu_cs_get_ring(adev, wait->in.ip_type, wait->in.ip_instance,
  911. wait->in.ring, &ring);
  912. if (r)
  913. return r;
  914. ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
  915. if (ctx == NULL)
  916. return -EINVAL;
  917. fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
  918. if (IS_ERR(fence))
  919. r = PTR_ERR(fence);
  920. else if (fence) {
  921. r = dma_fence_wait_timeout(fence, true, timeout);
  922. dma_fence_put(fence);
  923. } else
  924. r = 1;
  925. amdgpu_ctx_put(ctx);
  926. if (r < 0)
  927. return r;
  928. memset(wait, 0, sizeof(*wait));
  929. wait->out.status = (r == 0);
  930. return 0;
  931. }
  932. /**
  933. * amdgpu_cs_get_fence - helper to get fence from drm_amdgpu_fence
  934. *
  935. * @adev: amdgpu device
  936. * @filp: file private
  937. * @user: drm_amdgpu_fence copied from user space
  938. */
  939. static struct dma_fence *amdgpu_cs_get_fence(struct amdgpu_device *adev,
  940. struct drm_file *filp,
  941. struct drm_amdgpu_fence *user)
  942. {
  943. struct amdgpu_ring *ring;
  944. struct amdgpu_ctx *ctx;
  945. struct dma_fence *fence;
  946. int r;
  947. r = amdgpu_cs_get_ring(adev, user->ip_type, user->ip_instance,
  948. user->ring, &ring);
  949. if (r)
  950. return ERR_PTR(r);
  951. ctx = amdgpu_ctx_get(filp->driver_priv, user->ctx_id);
  952. if (ctx == NULL)
  953. return ERR_PTR(-EINVAL);
  954. fence = amdgpu_ctx_get_fence(ctx, ring, user->seq_no);
  955. amdgpu_ctx_put(ctx);
  956. return fence;
  957. }
  958. /**
  959. * amdgpu_cs_wait_all_fence - wait on all fences to signal
  960. *
  961. * @adev: amdgpu device
  962. * @filp: file private
  963. * @wait: wait parameters
  964. * @fences: array of drm_amdgpu_fence
  965. */
  966. static int amdgpu_cs_wait_all_fences(struct amdgpu_device *adev,
  967. struct drm_file *filp,
  968. union drm_amdgpu_wait_fences *wait,
  969. struct drm_amdgpu_fence *fences)
  970. {
  971. uint32_t fence_count = wait->in.fence_count;
  972. unsigned int i;
  973. long r = 1;
  974. for (i = 0; i < fence_count; i++) {
  975. struct dma_fence *fence;
  976. unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
  977. fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
  978. if (IS_ERR(fence))
  979. return PTR_ERR(fence);
  980. else if (!fence)
  981. continue;
  982. r = dma_fence_wait_timeout(fence, true, timeout);
  983. if (r < 0)
  984. return r;
  985. if (r == 0)
  986. break;
  987. }
  988. memset(wait, 0, sizeof(*wait));
  989. wait->out.status = (r > 0);
  990. return 0;
  991. }
  992. /**
  993. * amdgpu_cs_wait_any_fence - wait on any fence to signal
  994. *
  995. * @adev: amdgpu device
  996. * @filp: file private
  997. * @wait: wait parameters
  998. * @fences: array of drm_amdgpu_fence
  999. */
  1000. static int amdgpu_cs_wait_any_fence(struct amdgpu_device *adev,
  1001. struct drm_file *filp,
  1002. union drm_amdgpu_wait_fences *wait,
  1003. struct drm_amdgpu_fence *fences)
  1004. {
  1005. unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
  1006. uint32_t fence_count = wait->in.fence_count;
  1007. uint32_t first = ~0;
  1008. struct dma_fence **array;
  1009. unsigned int i;
  1010. long r;
  1011. /* Prepare the fence array */
  1012. array = kcalloc(fence_count, sizeof(struct dma_fence *), GFP_KERNEL);
  1013. if (array == NULL)
  1014. return -ENOMEM;
  1015. for (i = 0; i < fence_count; i++) {
  1016. struct dma_fence *fence;
  1017. fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
  1018. if (IS_ERR(fence)) {
  1019. r = PTR_ERR(fence);
  1020. goto err_free_fence_array;
  1021. } else if (fence) {
  1022. array[i] = fence;
  1023. } else { /* NULL, the fence has been already signaled */
  1024. r = 1;
  1025. goto out;
  1026. }
  1027. }
  1028. r = dma_fence_wait_any_timeout(array, fence_count, true, timeout,
  1029. &first);
  1030. if (r < 0)
  1031. goto err_free_fence_array;
  1032. out:
  1033. memset(wait, 0, sizeof(*wait));
  1034. wait->out.status = (r > 0);
  1035. wait->out.first_signaled = first;
  1036. /* set return value 0 to indicate success */
  1037. r = 0;
  1038. err_free_fence_array:
  1039. for (i = 0; i < fence_count; i++)
  1040. dma_fence_put(array[i]);
  1041. kfree(array);
  1042. return r;
  1043. }
  1044. /**
  1045. * amdgpu_cs_wait_fences_ioctl - wait for multiple command submissions to finish
  1046. *
  1047. * @dev: drm device
  1048. * @data: data from userspace
  1049. * @filp: file private
  1050. */
  1051. int amdgpu_cs_wait_fences_ioctl(struct drm_device *dev, void *data,
  1052. struct drm_file *filp)
  1053. {
  1054. struct amdgpu_device *adev = dev->dev_private;
  1055. union drm_amdgpu_wait_fences *wait = data;
  1056. uint32_t fence_count = wait->in.fence_count;
  1057. struct drm_amdgpu_fence *fences_user;
  1058. struct drm_amdgpu_fence *fences;
  1059. int r;
  1060. /* Get the fences from userspace */
  1061. fences = kmalloc_array(fence_count, sizeof(struct drm_amdgpu_fence),
  1062. GFP_KERNEL);
  1063. if (fences == NULL)
  1064. return -ENOMEM;
  1065. fences_user = (void __user *)(unsigned long)(wait->in.fences);
  1066. if (copy_from_user(fences, fences_user,
  1067. sizeof(struct drm_amdgpu_fence) * fence_count)) {
  1068. r = -EFAULT;
  1069. goto err_free_fences;
  1070. }
  1071. if (wait->in.wait_all)
  1072. r = amdgpu_cs_wait_all_fences(adev, filp, wait, fences);
  1073. else
  1074. r = amdgpu_cs_wait_any_fence(adev, filp, wait, fences);
  1075. err_free_fences:
  1076. kfree(fences);
  1077. return r;
  1078. }
  1079. /**
  1080. * amdgpu_cs_find_bo_va - find bo_va for VM address
  1081. *
  1082. * @parser: command submission parser context
  1083. * @addr: VM address
  1084. * @bo: resulting BO of the mapping found
  1085. *
  1086. * Search the buffer objects in the command submission context for a certain
  1087. * virtual memory address. Returns allocation structure when found, NULL
  1088. * otherwise.
  1089. */
  1090. struct amdgpu_bo_va_mapping *
  1091. amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
  1092. uint64_t addr, struct amdgpu_bo **bo)
  1093. {
  1094. struct amdgpu_bo_va_mapping *mapping;
  1095. unsigned i;
  1096. if (!parser->bo_list)
  1097. return NULL;
  1098. addr /= AMDGPU_GPU_PAGE_SIZE;
  1099. for (i = 0; i < parser->bo_list->num_entries; i++) {
  1100. struct amdgpu_bo_list_entry *lobj;
  1101. lobj = &parser->bo_list->array[i];
  1102. if (!lobj->bo_va)
  1103. continue;
  1104. list_for_each_entry(mapping, &lobj->bo_va->valids, list) {
  1105. if (mapping->it.start > addr ||
  1106. addr > mapping->it.last)
  1107. continue;
  1108. *bo = lobj->bo_va->bo;
  1109. return mapping;
  1110. }
  1111. list_for_each_entry(mapping, &lobj->bo_va->invalids, list) {
  1112. if (mapping->it.start > addr ||
  1113. addr > mapping->it.last)
  1114. continue;
  1115. *bo = lobj->bo_va->bo;
  1116. return mapping;
  1117. }
  1118. }
  1119. return NULL;
  1120. }
  1121. /**
  1122. * amdgpu_cs_sysvm_access_required - make BOs accessible by the system VM
  1123. *
  1124. * @parser: command submission parser context
  1125. *
  1126. * Helper for UVD/VCE VM emulation, make sure BOs are accessible by the system VM.
  1127. */
  1128. int amdgpu_cs_sysvm_access_required(struct amdgpu_cs_parser *parser)
  1129. {
  1130. unsigned i;
  1131. int r;
  1132. if (!parser->bo_list)
  1133. return 0;
  1134. for (i = 0; i < parser->bo_list->num_entries; i++) {
  1135. struct amdgpu_bo *bo = parser->bo_list->array[i].robj;
  1136. r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem);
  1137. if (unlikely(r))
  1138. return r;
  1139. if (bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)
  1140. continue;
  1141. bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
  1142. amdgpu_ttm_placement_from_domain(bo, bo->allowed_domains);
  1143. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
  1144. if (unlikely(r))
  1145. return r;
  1146. }
  1147. return 0;
  1148. }