amdgpu_cs.c 40 KB

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