amdgpu_cs.c 39 KB

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