amdgpu_cs.c 35 KB

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