amdgpu_cs.c 39 KB

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