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