amdgpu_cs.c 40 KB

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