amdgpu_cs.c 40 KB

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