amdgpu_cs.c 37 KB

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