amdgpu_cs.c 40 KB

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