amdgpu_cs.c 39 KB

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