amdgpu_cs.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  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 <drm/drmP.h>
  29. #include <drm/amdgpu_drm.h>
  30. #include "amdgpu.h"
  31. #include "amdgpu_trace.h"
  32. int amdgpu_cs_get_ring(struct amdgpu_device *adev, u32 ip_type,
  33. u32 ip_instance, u32 ring,
  34. struct amdgpu_ring **out_ring)
  35. {
  36. /* Right now all IPs have only one instance - multiple rings. */
  37. if (ip_instance != 0) {
  38. DRM_ERROR("invalid ip instance: %d\n", ip_instance);
  39. return -EINVAL;
  40. }
  41. switch (ip_type) {
  42. default:
  43. DRM_ERROR("unknown ip type: %d\n", ip_type);
  44. return -EINVAL;
  45. case AMDGPU_HW_IP_GFX:
  46. if (ring < adev->gfx.num_gfx_rings) {
  47. *out_ring = &adev->gfx.gfx_ring[ring];
  48. } else {
  49. DRM_ERROR("only %d gfx rings are supported now\n",
  50. adev->gfx.num_gfx_rings);
  51. return -EINVAL;
  52. }
  53. break;
  54. case AMDGPU_HW_IP_COMPUTE:
  55. if (ring < adev->gfx.num_compute_rings) {
  56. *out_ring = &adev->gfx.compute_ring[ring];
  57. } else {
  58. DRM_ERROR("only %d compute rings are supported now\n",
  59. adev->gfx.num_compute_rings);
  60. return -EINVAL;
  61. }
  62. break;
  63. case AMDGPU_HW_IP_DMA:
  64. if (ring < adev->sdma.num_instances) {
  65. *out_ring = &adev->sdma.instance[ring].ring;
  66. } else {
  67. DRM_ERROR("only %d SDMA rings are supported\n",
  68. adev->sdma.num_instances);
  69. return -EINVAL;
  70. }
  71. break;
  72. case AMDGPU_HW_IP_UVD:
  73. *out_ring = &adev->uvd.ring;
  74. break;
  75. case AMDGPU_HW_IP_VCE:
  76. if (ring < 2){
  77. *out_ring = &adev->vce.ring[ring];
  78. } else {
  79. DRM_ERROR("only two VCE rings are supported\n");
  80. return -EINVAL;
  81. }
  82. break;
  83. }
  84. return 0;
  85. }
  86. static int amdgpu_cs_user_fence_chunk(struct amdgpu_cs_parser *p,
  87. struct drm_amdgpu_cs_chunk_fence *data,
  88. uint32_t *offset)
  89. {
  90. struct drm_gem_object *gobj;
  91. unsigned long size;
  92. gobj = drm_gem_object_lookup(p->filp, data->handle);
  93. if (gobj == NULL)
  94. return -EINVAL;
  95. p->uf_entry.robj = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
  96. p->uf_entry.priority = 0;
  97. p->uf_entry.tv.bo = &p->uf_entry.robj->tbo;
  98. p->uf_entry.tv.shared = true;
  99. p->uf_entry.user_pages = NULL;
  100. size = amdgpu_bo_size(p->uf_entry.robj);
  101. if (size != PAGE_SIZE || (data->offset + 8) > size)
  102. return -EINVAL;
  103. *offset = data->offset;
  104. drm_gem_object_unreference_unlocked(gobj);
  105. if (amdgpu_ttm_tt_get_usermm(p->uf_entry.robj->tbo.ttm)) {
  106. amdgpu_bo_unref(&p->uf_entry.robj);
  107. return -EINVAL;
  108. }
  109. return 0;
  110. }
  111. int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, void *data)
  112. {
  113. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  114. struct amdgpu_vm *vm = &fpriv->vm;
  115. union drm_amdgpu_cs *cs = data;
  116. uint64_t *chunk_array_user;
  117. uint64_t *chunk_array;
  118. unsigned size, num_ibs = 0;
  119. uint32_t uf_offset = 0;
  120. int i;
  121. int ret;
  122. if (cs->in.num_chunks == 0)
  123. return 0;
  124. chunk_array = kmalloc_array(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL);
  125. if (!chunk_array)
  126. return -ENOMEM;
  127. p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id);
  128. if (!p->ctx) {
  129. ret = -EINVAL;
  130. goto free_chunk;
  131. }
  132. /* get chunks */
  133. chunk_array_user = (uint64_t __user *)(unsigned long)(cs->in.chunks);
  134. if (copy_from_user(chunk_array, chunk_array_user,
  135. sizeof(uint64_t)*cs->in.num_chunks)) {
  136. ret = -EFAULT;
  137. goto put_ctx;
  138. }
  139. p->nchunks = cs->in.num_chunks;
  140. p->chunks = kmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk),
  141. GFP_KERNEL);
  142. if (!p->chunks) {
  143. ret = -ENOMEM;
  144. goto put_ctx;
  145. }
  146. for (i = 0; i < p->nchunks; i++) {
  147. struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL;
  148. struct drm_amdgpu_cs_chunk user_chunk;
  149. uint32_t __user *cdata;
  150. chunk_ptr = (void __user *)(unsigned long)chunk_array[i];
  151. if (copy_from_user(&user_chunk, chunk_ptr,
  152. sizeof(struct drm_amdgpu_cs_chunk))) {
  153. ret = -EFAULT;
  154. i--;
  155. goto free_partial_kdata;
  156. }
  157. p->chunks[i].chunk_id = user_chunk.chunk_id;
  158. p->chunks[i].length_dw = user_chunk.length_dw;
  159. size = p->chunks[i].length_dw;
  160. cdata = (void __user *)(unsigned long)user_chunk.chunk_data;
  161. p->chunks[i].kdata = drm_malloc_ab(size, sizeof(uint32_t));
  162. if (p->chunks[i].kdata == NULL) {
  163. ret = -ENOMEM;
  164. i--;
  165. goto free_partial_kdata;
  166. }
  167. size *= sizeof(uint32_t);
  168. if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
  169. ret = -EFAULT;
  170. goto free_partial_kdata;
  171. }
  172. switch (p->chunks[i].chunk_id) {
  173. case AMDGPU_CHUNK_ID_IB:
  174. ++num_ibs;
  175. break;
  176. case AMDGPU_CHUNK_ID_FENCE:
  177. size = sizeof(struct drm_amdgpu_cs_chunk_fence);
  178. if (p->chunks[i].length_dw * sizeof(uint32_t) < size) {
  179. ret = -EINVAL;
  180. goto free_partial_kdata;
  181. }
  182. ret = amdgpu_cs_user_fence_chunk(p, p->chunks[i].kdata,
  183. &uf_offset);
  184. if (ret)
  185. goto free_partial_kdata;
  186. break;
  187. case AMDGPU_CHUNK_ID_DEPENDENCIES:
  188. break;
  189. default:
  190. ret = -EINVAL;
  191. goto free_partial_kdata;
  192. }
  193. }
  194. ret = amdgpu_job_alloc(p->adev, num_ibs, &p->job, vm);
  195. if (ret)
  196. goto free_all_kdata;
  197. if (p->uf_entry.robj)
  198. p->job->uf_addr = uf_offset;
  199. kfree(chunk_array);
  200. return 0;
  201. free_all_kdata:
  202. i = p->nchunks - 1;
  203. free_partial_kdata:
  204. for (; i >= 0; i--)
  205. drm_free_large(p->chunks[i].kdata);
  206. kfree(p->chunks);
  207. put_ctx:
  208. amdgpu_ctx_put(p->ctx);
  209. free_chunk:
  210. kfree(chunk_array);
  211. return ret;
  212. }
  213. /* Convert microseconds to bytes. */
  214. static u64 us_to_bytes(struct amdgpu_device *adev, s64 us)
  215. {
  216. if (us <= 0 || !adev->mm_stats.log2_max_MBps)
  217. return 0;
  218. /* Since accum_us is incremented by a million per second, just
  219. * multiply it by the number of MB/s to get the number of bytes.
  220. */
  221. return us << adev->mm_stats.log2_max_MBps;
  222. }
  223. static s64 bytes_to_us(struct amdgpu_device *adev, u64 bytes)
  224. {
  225. if (!adev->mm_stats.log2_max_MBps)
  226. return 0;
  227. return bytes >> adev->mm_stats.log2_max_MBps;
  228. }
  229. /* Returns how many bytes TTM can move right now. If no bytes can be moved,
  230. * it returns 0. If it returns non-zero, it's OK to move at least one buffer,
  231. * which means it can go over the threshold once. If that happens, the driver
  232. * will be in debt and no other buffer migrations can be done until that debt
  233. * is repaid.
  234. *
  235. * This approach allows moving a buffer of any size (it's important to allow
  236. * that).
  237. *
  238. * The currency is simply time in microseconds and it increases as the clock
  239. * ticks. The accumulated microseconds (us) are converted to bytes and
  240. * returned.
  241. */
  242. static u64 amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev)
  243. {
  244. s64 time_us, increment_us;
  245. u64 max_bytes;
  246. u64 free_vram, total_vram, used_vram;
  247. /* Allow a maximum of 200 accumulated ms. This is basically per-IB
  248. * throttling.
  249. *
  250. * It means that in order to get full max MBps, at least 5 IBs per
  251. * second must be submitted and not more than 200ms apart from each
  252. * other.
  253. */
  254. const s64 us_upper_bound = 200000;
  255. if (!adev->mm_stats.log2_max_MBps)
  256. return 0;
  257. total_vram = adev->mc.real_vram_size - adev->vram_pin_size;
  258. used_vram = atomic64_read(&adev->vram_usage);
  259. free_vram = used_vram >= total_vram ? 0 : total_vram - used_vram;
  260. spin_lock(&adev->mm_stats.lock);
  261. /* Increase the amount of accumulated us. */
  262. time_us = ktime_to_us(ktime_get());
  263. increment_us = time_us - adev->mm_stats.last_update_us;
  264. adev->mm_stats.last_update_us = time_us;
  265. adev->mm_stats.accum_us = min(adev->mm_stats.accum_us + increment_us,
  266. us_upper_bound);
  267. /* This prevents the short period of low performance when the VRAM
  268. * usage is low and the driver is in debt or doesn't have enough
  269. * accumulated us to fill VRAM quickly.
  270. *
  271. * The situation can occur in these cases:
  272. * - a lot of VRAM is freed by userspace
  273. * - the presence of a big buffer causes a lot of evictions
  274. * (solution: split buffers into smaller ones)
  275. *
  276. * If 128 MB or 1/8th of VRAM is free, start filling it now by setting
  277. * accum_us to a positive number.
  278. */
  279. if (free_vram >= 128 * 1024 * 1024 || free_vram >= total_vram / 8) {
  280. s64 min_us;
  281. /* Be more aggresive on dGPUs. Try to fill a portion of free
  282. * VRAM now.
  283. */
  284. if (!(adev->flags & AMD_IS_APU))
  285. min_us = bytes_to_us(adev, free_vram / 4);
  286. else
  287. min_us = 0; /* Reset accum_us on APUs. */
  288. adev->mm_stats.accum_us = max(min_us, adev->mm_stats.accum_us);
  289. }
  290. /* This returns 0 if the driver is in debt to disallow (optional)
  291. * buffer moves.
  292. */
  293. max_bytes = us_to_bytes(adev, adev->mm_stats.accum_us);
  294. spin_unlock(&adev->mm_stats.lock);
  295. return max_bytes;
  296. }
  297. /* Report how many bytes have really been moved for the last command
  298. * submission. This can result in a debt that can stop buffer migrations
  299. * temporarily.
  300. */
  301. static void amdgpu_cs_report_moved_bytes(struct amdgpu_device *adev,
  302. u64 num_bytes)
  303. {
  304. spin_lock(&adev->mm_stats.lock);
  305. adev->mm_stats.accum_us -= bytes_to_us(adev, num_bytes);
  306. spin_unlock(&adev->mm_stats.lock);
  307. }
  308. static int amdgpu_cs_bo_validate(struct amdgpu_cs_parser *p,
  309. struct amdgpu_bo *bo)
  310. {
  311. u64 initial_bytes_moved;
  312. uint32_t domain;
  313. int r;
  314. if (bo->pin_count)
  315. return 0;
  316. /* Don't move this buffer if we have depleted our allowance
  317. * to move it. Don't move anything if the threshold is zero.
  318. */
  319. if (p->bytes_moved < p->bytes_moved_threshold)
  320. domain = bo->prefered_domains;
  321. else
  322. domain = bo->allowed_domains;
  323. retry:
  324. amdgpu_ttm_placement_from_domain(bo, domain);
  325. initial_bytes_moved = atomic64_read(&bo->adev->num_bytes_moved);
  326. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  327. p->bytes_moved += atomic64_read(&bo->adev->num_bytes_moved) -
  328. initial_bytes_moved;
  329. if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) {
  330. domain = bo->allowed_domains;
  331. goto retry;
  332. }
  333. return r;
  334. }
  335. /* Last resort, try to evict something from the current working set */
  336. static bool amdgpu_cs_try_evict(struct amdgpu_cs_parser *p,
  337. struct amdgpu_bo_list_entry *lobj)
  338. {
  339. uint32_t domain = lobj->robj->allowed_domains;
  340. int r;
  341. if (!p->evictable)
  342. return false;
  343. for (;&p->evictable->tv.head != &p->validated;
  344. p->evictable = list_prev_entry(p->evictable, tv.head)) {
  345. struct amdgpu_bo_list_entry *candidate = p->evictable;
  346. struct amdgpu_bo *bo = candidate->robj;
  347. u64 initial_bytes_moved;
  348. uint32_t other;
  349. /* If we reached our current BO we can forget it */
  350. if (candidate == lobj)
  351. break;
  352. other = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
  353. /* Check if this BO is in one of the domains we need space for */
  354. if (!(other & domain))
  355. continue;
  356. /* Check if we can move this BO somewhere else */
  357. other = bo->allowed_domains & ~domain;
  358. if (!other)
  359. continue;
  360. /* Good we can try to move this BO somewhere else */
  361. amdgpu_ttm_placement_from_domain(bo, other);
  362. initial_bytes_moved = atomic64_read(&bo->adev->num_bytes_moved);
  363. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  364. p->bytes_moved += atomic64_read(&bo->adev->num_bytes_moved) -
  365. initial_bytes_moved;
  366. if (unlikely(r))
  367. break;
  368. p->evictable = list_prev_entry(p->evictable, tv.head);
  369. list_move(&candidate->tv.head, &p->validated);
  370. return true;
  371. }
  372. return false;
  373. }
  374. static int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p,
  375. struct list_head *validated)
  376. {
  377. struct amdgpu_bo_list_entry *lobj;
  378. int r;
  379. list_for_each_entry(lobj, validated, tv.head) {
  380. struct amdgpu_bo *bo = lobj->robj;
  381. bool binding_userptr = false;
  382. struct mm_struct *usermm;
  383. usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
  384. if (usermm && usermm != current->mm)
  385. return -EPERM;
  386. /* Check if we have user pages and nobody bound the BO already */
  387. if (lobj->user_pages && bo->tbo.ttm->state != tt_bound) {
  388. size_t size = sizeof(struct page *);
  389. size *= bo->tbo.ttm->num_pages;
  390. memcpy(bo->tbo.ttm->pages, lobj->user_pages, size);
  391. binding_userptr = true;
  392. }
  393. if (p->evictable == lobj)
  394. p->evictable = NULL;
  395. do {
  396. r = amdgpu_cs_bo_validate(p, bo);
  397. } while (r == -ENOMEM && amdgpu_cs_try_evict(p, lobj));
  398. if (r)
  399. return r;
  400. if (bo->shadow) {
  401. r = amdgpu_cs_bo_validate(p, bo);
  402. if (r)
  403. return r;
  404. }
  405. if (binding_userptr) {
  406. drm_free_large(lobj->user_pages);
  407. lobj->user_pages = NULL;
  408. }
  409. }
  410. return 0;
  411. }
  412. static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
  413. union drm_amdgpu_cs *cs)
  414. {
  415. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  416. struct amdgpu_bo_list_entry *e;
  417. struct list_head duplicates;
  418. bool need_mmap_lock = false;
  419. unsigned i, tries = 10;
  420. int r;
  421. INIT_LIST_HEAD(&p->validated);
  422. p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
  423. if (p->bo_list) {
  424. need_mmap_lock = p->bo_list->first_userptr !=
  425. p->bo_list->num_entries;
  426. amdgpu_bo_list_get_list(p->bo_list, &p->validated);
  427. }
  428. INIT_LIST_HEAD(&duplicates);
  429. amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
  430. if (p->uf_entry.robj)
  431. list_add(&p->uf_entry.tv.head, &p->validated);
  432. if (need_mmap_lock)
  433. down_read(&current->mm->mmap_sem);
  434. while (1) {
  435. struct list_head need_pages;
  436. unsigned i;
  437. r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
  438. &duplicates);
  439. if (unlikely(r != 0)) {
  440. if (r != -ERESTARTSYS)
  441. DRM_ERROR("ttm_eu_reserve_buffers failed.\n");
  442. goto error_free_pages;
  443. }
  444. /* Without a BO list we don't have userptr BOs */
  445. if (!p->bo_list)
  446. break;
  447. INIT_LIST_HEAD(&need_pages);
  448. for (i = p->bo_list->first_userptr;
  449. i < p->bo_list->num_entries; ++i) {
  450. e = &p->bo_list->array[i];
  451. if (amdgpu_ttm_tt_userptr_invalidated(e->robj->tbo.ttm,
  452. &e->user_invalidated) && e->user_pages) {
  453. /* We acquired a page array, but somebody
  454. * invalidated it. Free it an try again
  455. */
  456. release_pages(e->user_pages,
  457. e->robj->tbo.ttm->num_pages,
  458. false);
  459. drm_free_large(e->user_pages);
  460. e->user_pages = NULL;
  461. }
  462. if (e->robj->tbo.ttm->state != tt_bound &&
  463. !e->user_pages) {
  464. list_del(&e->tv.head);
  465. list_add(&e->tv.head, &need_pages);
  466. amdgpu_bo_unreserve(e->robj);
  467. }
  468. }
  469. if (list_empty(&need_pages))
  470. break;
  471. /* Unreserve everything again. */
  472. ttm_eu_backoff_reservation(&p->ticket, &p->validated);
  473. /* We tried too many times, just abort */
  474. if (!--tries) {
  475. r = -EDEADLK;
  476. DRM_ERROR("deadlock in %s\n", __func__);
  477. goto error_free_pages;
  478. }
  479. /* Fill the page arrays for all useptrs. */
  480. list_for_each_entry(e, &need_pages, tv.head) {
  481. struct ttm_tt *ttm = e->robj->tbo.ttm;
  482. e->user_pages = drm_calloc_large(ttm->num_pages,
  483. sizeof(struct page*));
  484. if (!e->user_pages) {
  485. r = -ENOMEM;
  486. DRM_ERROR("calloc failure in %s\n", __func__);
  487. goto error_free_pages;
  488. }
  489. r = amdgpu_ttm_tt_get_user_pages(ttm, e->user_pages);
  490. if (r) {
  491. DRM_ERROR("amdgpu_ttm_tt_get_user_pages failed.\n");
  492. drm_free_large(e->user_pages);
  493. e->user_pages = NULL;
  494. goto error_free_pages;
  495. }
  496. }
  497. /* And try again. */
  498. list_splice(&need_pages, &p->validated);
  499. }
  500. amdgpu_vm_get_pt_bos(p->adev, &fpriv->vm, &duplicates);
  501. p->bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(p->adev);
  502. p->bytes_moved = 0;
  503. p->evictable = list_last_entry(&p->validated,
  504. struct amdgpu_bo_list_entry,
  505. tv.head);
  506. r = amdgpu_cs_list_validate(p, &duplicates);
  507. if (r) {
  508. DRM_ERROR("amdgpu_cs_list_validate(duplicates) failed.\n");
  509. goto error_validate;
  510. }
  511. r = amdgpu_cs_list_validate(p, &p->validated);
  512. if (r) {
  513. DRM_ERROR("amdgpu_cs_list_validate(validated) failed.\n");
  514. goto error_validate;
  515. }
  516. amdgpu_cs_report_moved_bytes(p->adev, p->bytes_moved);
  517. fpriv->vm.last_eviction_counter =
  518. atomic64_read(&p->adev->num_evictions);
  519. if (p->bo_list) {
  520. struct amdgpu_bo *gds = p->bo_list->gds_obj;
  521. struct amdgpu_bo *gws = p->bo_list->gws_obj;
  522. struct amdgpu_bo *oa = p->bo_list->oa_obj;
  523. struct amdgpu_vm *vm = &fpriv->vm;
  524. unsigned i;
  525. for (i = 0; i < p->bo_list->num_entries; i++) {
  526. struct amdgpu_bo *bo = p->bo_list->array[i].robj;
  527. p->bo_list->array[i].bo_va = amdgpu_vm_bo_find(vm, bo);
  528. }
  529. if (gds) {
  530. p->job->gds_base = amdgpu_bo_gpu_offset(gds);
  531. p->job->gds_size = amdgpu_bo_size(gds);
  532. }
  533. if (gws) {
  534. p->job->gws_base = amdgpu_bo_gpu_offset(gws);
  535. p->job->gws_size = amdgpu_bo_size(gws);
  536. }
  537. if (oa) {
  538. p->job->oa_base = amdgpu_bo_gpu_offset(oa);
  539. p->job->oa_size = amdgpu_bo_size(oa);
  540. }
  541. }
  542. if (!r && p->uf_entry.robj) {
  543. struct amdgpu_bo *uf = p->uf_entry.robj;
  544. r = amdgpu_ttm_bind(&uf->tbo, &uf->tbo.mem);
  545. p->job->uf_addr += amdgpu_bo_gpu_offset(uf);
  546. }
  547. error_validate:
  548. if (r) {
  549. amdgpu_vm_move_pt_bos_in_lru(p->adev, &fpriv->vm);
  550. ttm_eu_backoff_reservation(&p->ticket, &p->validated);
  551. }
  552. error_free_pages:
  553. if (need_mmap_lock)
  554. up_read(&current->mm->mmap_sem);
  555. if (p->bo_list) {
  556. for (i = p->bo_list->first_userptr;
  557. i < p->bo_list->num_entries; ++i) {
  558. e = &p->bo_list->array[i];
  559. if (!e->user_pages)
  560. continue;
  561. release_pages(e->user_pages,
  562. e->robj->tbo.ttm->num_pages,
  563. false);
  564. drm_free_large(e->user_pages);
  565. }
  566. }
  567. return r;
  568. }
  569. static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
  570. {
  571. struct amdgpu_bo_list_entry *e;
  572. int r;
  573. list_for_each_entry(e, &p->validated, tv.head) {
  574. struct reservation_object *resv = e->robj->tbo.resv;
  575. r = amdgpu_sync_resv(p->adev, &p->job->sync, resv, p->filp);
  576. if (r)
  577. return r;
  578. }
  579. return 0;
  580. }
  581. /**
  582. * cs_parser_fini() - clean parser states
  583. * @parser: parser structure holding parsing context.
  584. * @error: error number
  585. *
  586. * If error is set than unvalidate buffer, otherwise just free memory
  587. * used by parsing context.
  588. **/
  589. static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
  590. {
  591. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  592. unsigned i;
  593. if (!error) {
  594. amdgpu_vm_move_pt_bos_in_lru(parser->adev, &fpriv->vm);
  595. ttm_eu_fence_buffer_objects(&parser->ticket,
  596. &parser->validated,
  597. parser->fence);
  598. } else if (backoff) {
  599. ttm_eu_backoff_reservation(&parser->ticket,
  600. &parser->validated);
  601. }
  602. fence_put(parser->fence);
  603. if (parser->ctx)
  604. amdgpu_ctx_put(parser->ctx);
  605. if (parser->bo_list)
  606. amdgpu_bo_list_put(parser->bo_list);
  607. for (i = 0; i < parser->nchunks; i++)
  608. drm_free_large(parser->chunks[i].kdata);
  609. kfree(parser->chunks);
  610. if (parser->job)
  611. amdgpu_job_free(parser->job);
  612. amdgpu_bo_unref(&parser->uf_entry.robj);
  613. }
  614. static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p,
  615. struct amdgpu_vm *vm)
  616. {
  617. struct amdgpu_device *adev = p->adev;
  618. struct amdgpu_bo_va *bo_va;
  619. struct amdgpu_bo *bo;
  620. int i, r;
  621. r = amdgpu_vm_update_page_directory(adev, vm);
  622. if (r)
  623. return r;
  624. r = amdgpu_sync_fence(adev, &p->job->sync, vm->page_directory_fence);
  625. if (r)
  626. return r;
  627. r = amdgpu_vm_clear_freed(adev, vm);
  628. if (r)
  629. return r;
  630. if (p->bo_list) {
  631. for (i = 0; i < p->bo_list->num_entries; i++) {
  632. struct fence *f;
  633. /* ignore duplicates */
  634. bo = p->bo_list->array[i].robj;
  635. if (!bo)
  636. continue;
  637. bo_va = p->bo_list->array[i].bo_va;
  638. if (bo_va == NULL)
  639. continue;
  640. r = amdgpu_vm_bo_update(adev, bo_va, false);
  641. if (r)
  642. return r;
  643. f = bo_va->last_pt_update;
  644. r = amdgpu_sync_fence(adev, &p->job->sync, f);
  645. if (r)
  646. return r;
  647. }
  648. }
  649. r = amdgpu_vm_clear_invalids(adev, vm, &p->job->sync);
  650. if (amdgpu_vm_debug && p->bo_list) {
  651. /* Invalidate all BOs to test for userspace bugs */
  652. for (i = 0; i < p->bo_list->num_entries; i++) {
  653. /* ignore duplicates */
  654. bo = p->bo_list->array[i].robj;
  655. if (!bo)
  656. continue;
  657. amdgpu_vm_bo_invalidate(adev, bo);
  658. }
  659. }
  660. return r;
  661. }
  662. static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
  663. struct amdgpu_cs_parser *p)
  664. {
  665. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  666. struct amdgpu_vm *vm = &fpriv->vm;
  667. struct amdgpu_ring *ring = p->job->ring;
  668. int i, r;
  669. /* Only for UVD/VCE VM emulation */
  670. if (ring->funcs->parse_cs) {
  671. p->job->vm = NULL;
  672. for (i = 0; i < p->job->num_ibs; i++) {
  673. r = amdgpu_ring_parse_cs(ring, p, i);
  674. if (r)
  675. return r;
  676. }
  677. } else {
  678. p->job->vm_pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
  679. r = amdgpu_bo_vm_update_pte(p, vm);
  680. if (r)
  681. return r;
  682. }
  683. return amdgpu_cs_sync_rings(p);
  684. }
  685. static int amdgpu_cs_handle_lockup(struct amdgpu_device *adev, int r)
  686. {
  687. if (r == -EDEADLK) {
  688. r = amdgpu_gpu_reset(adev);
  689. if (!r)
  690. r = -EAGAIN;
  691. }
  692. return r;
  693. }
  694. static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
  695. struct amdgpu_cs_parser *parser)
  696. {
  697. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  698. struct amdgpu_vm *vm = &fpriv->vm;
  699. int i, j;
  700. int r;
  701. for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) {
  702. struct amdgpu_cs_chunk *chunk;
  703. struct amdgpu_ib *ib;
  704. struct drm_amdgpu_cs_chunk_ib *chunk_ib;
  705. struct amdgpu_ring *ring;
  706. chunk = &parser->chunks[i];
  707. ib = &parser->job->ibs[j];
  708. chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
  709. if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
  710. continue;
  711. r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
  712. chunk_ib->ip_instance, chunk_ib->ring,
  713. &ring);
  714. if (r)
  715. return r;
  716. if (ib->flags & AMDGPU_IB_FLAG_PREAMBLE) {
  717. parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT;
  718. if (!parser->ctx->preamble_presented) {
  719. parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT_FIRST;
  720. parser->ctx->preamble_presented = true;
  721. }
  722. }
  723. if (parser->job->ring && parser->job->ring != ring)
  724. return -EINVAL;
  725. parser->job->ring = ring;
  726. if (ring->funcs->parse_cs) {
  727. struct amdgpu_bo_va_mapping *m;
  728. struct amdgpu_bo *aobj = NULL;
  729. uint64_t offset;
  730. uint8_t *kptr;
  731. m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
  732. &aobj);
  733. if (!aobj) {
  734. DRM_ERROR("IB va_start is invalid\n");
  735. return -EINVAL;
  736. }
  737. if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
  738. (m->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
  739. DRM_ERROR("IB va_start+ib_bytes is invalid\n");
  740. return -EINVAL;
  741. }
  742. /* the IB should be reserved at this point */
  743. r = amdgpu_bo_kmap(aobj, (void **)&kptr);
  744. if (r) {
  745. return r;
  746. }
  747. offset = ((uint64_t)m->it.start) * AMDGPU_GPU_PAGE_SIZE;
  748. kptr += chunk_ib->va_start - offset;
  749. r = amdgpu_ib_get(adev, NULL, chunk_ib->ib_bytes, ib);
  750. if (r) {
  751. DRM_ERROR("Failed to get ib !\n");
  752. return r;
  753. }
  754. memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
  755. amdgpu_bo_kunmap(aobj);
  756. } else {
  757. r = amdgpu_ib_get(adev, vm, 0, ib);
  758. if (r) {
  759. DRM_ERROR("Failed to get ib !\n");
  760. return r;
  761. }
  762. ib->gpu_addr = chunk_ib->va_start;
  763. }
  764. ib->length_dw = chunk_ib->ib_bytes / 4;
  765. ib->flags = chunk_ib->flags;
  766. j++;
  767. }
  768. /* UVD & VCE fw doesn't support user fences */
  769. if (parser->job->uf_addr && (
  770. parser->job->ring->type == AMDGPU_RING_TYPE_UVD ||
  771. parser->job->ring->type == AMDGPU_RING_TYPE_VCE))
  772. return -EINVAL;
  773. return 0;
  774. }
  775. static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
  776. struct amdgpu_cs_parser *p)
  777. {
  778. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  779. int i, j, r;
  780. for (i = 0; i < p->nchunks; ++i) {
  781. struct drm_amdgpu_cs_chunk_dep *deps;
  782. struct amdgpu_cs_chunk *chunk;
  783. unsigned num_deps;
  784. chunk = &p->chunks[i];
  785. if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES)
  786. continue;
  787. deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
  788. num_deps = chunk->length_dw * 4 /
  789. sizeof(struct drm_amdgpu_cs_chunk_dep);
  790. for (j = 0; j < num_deps; ++j) {
  791. struct amdgpu_ring *ring;
  792. struct amdgpu_ctx *ctx;
  793. struct fence *fence;
  794. r = amdgpu_cs_get_ring(adev, deps[j].ip_type,
  795. deps[j].ip_instance,
  796. deps[j].ring, &ring);
  797. if (r)
  798. return r;
  799. ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id);
  800. if (ctx == NULL)
  801. return -EINVAL;
  802. fence = amdgpu_ctx_get_fence(ctx, ring,
  803. deps[j].handle);
  804. if (IS_ERR(fence)) {
  805. r = PTR_ERR(fence);
  806. amdgpu_ctx_put(ctx);
  807. return r;
  808. } else if (fence) {
  809. r = amdgpu_sync_fence(adev, &p->job->sync,
  810. fence);
  811. fence_put(fence);
  812. amdgpu_ctx_put(ctx);
  813. if (r)
  814. return r;
  815. }
  816. }
  817. }
  818. return 0;
  819. }
  820. static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
  821. union drm_amdgpu_cs *cs)
  822. {
  823. struct amdgpu_ring *ring = p->job->ring;
  824. struct amd_sched_entity *entity = &p->ctx->rings[ring->idx].entity;
  825. struct amdgpu_job *job;
  826. int r;
  827. job = p->job;
  828. p->job = NULL;
  829. r = amd_sched_job_init(&job->base, &ring->sched, entity, p->filp);
  830. if (r) {
  831. amdgpu_job_free(job);
  832. return r;
  833. }
  834. job->owner = p->filp;
  835. job->fence_ctx = entity->fence_context;
  836. p->fence = fence_get(&job->base.s_fence->finished);
  837. cs->out.handle = amdgpu_ctx_add_fence(p->ctx, ring, p->fence);
  838. job->uf_sequence = cs->out.handle;
  839. amdgpu_job_free_resources(job);
  840. trace_amdgpu_cs_ioctl(job);
  841. amd_sched_entity_push_job(&job->base);
  842. return 0;
  843. }
  844. int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  845. {
  846. struct amdgpu_device *adev = dev->dev_private;
  847. union drm_amdgpu_cs *cs = data;
  848. struct amdgpu_cs_parser parser = {};
  849. bool reserved_buffers = false;
  850. int i, r;
  851. if (!adev->accel_working)
  852. return -EBUSY;
  853. parser.adev = adev;
  854. parser.filp = filp;
  855. r = amdgpu_cs_parser_init(&parser, data);
  856. if (r) {
  857. DRM_ERROR("Failed to initialize parser !\n");
  858. amdgpu_cs_parser_fini(&parser, r, false);
  859. r = amdgpu_cs_handle_lockup(adev, r);
  860. return r;
  861. }
  862. r = amdgpu_cs_parser_bos(&parser, data);
  863. if (r == -ENOMEM)
  864. DRM_ERROR("Not enough memory for command submission!\n");
  865. else if (r && r != -ERESTARTSYS)
  866. DRM_ERROR("Failed to process the buffer list %d!\n", r);
  867. else if (!r) {
  868. reserved_buffers = true;
  869. r = amdgpu_cs_ib_fill(adev, &parser);
  870. }
  871. if (!r) {
  872. r = amdgpu_cs_dependencies(adev, &parser);
  873. if (r)
  874. DRM_ERROR("Failed in the dependencies handling %d!\n", r);
  875. }
  876. if (r)
  877. goto out;
  878. for (i = 0; i < parser.job->num_ibs; i++)
  879. trace_amdgpu_cs(&parser, i);
  880. r = amdgpu_cs_ib_vm_chunk(adev, &parser);
  881. if (r)
  882. goto out;
  883. r = amdgpu_cs_submit(&parser, cs);
  884. out:
  885. amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
  886. r = amdgpu_cs_handle_lockup(adev, r);
  887. return r;
  888. }
  889. /**
  890. * amdgpu_cs_wait_ioctl - wait for a command submission to finish
  891. *
  892. * @dev: drm device
  893. * @data: data from userspace
  894. * @filp: file private
  895. *
  896. * Wait for the command submission identified by handle to finish.
  897. */
  898. int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
  899. struct drm_file *filp)
  900. {
  901. union drm_amdgpu_wait_cs *wait = data;
  902. struct amdgpu_device *adev = dev->dev_private;
  903. unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
  904. struct amdgpu_ring *ring = NULL;
  905. struct amdgpu_ctx *ctx;
  906. struct fence *fence;
  907. long r;
  908. r = amdgpu_cs_get_ring(adev, wait->in.ip_type, wait->in.ip_instance,
  909. wait->in.ring, &ring);
  910. if (r)
  911. return r;
  912. ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
  913. if (ctx == NULL)
  914. return -EINVAL;
  915. fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
  916. if (IS_ERR(fence))
  917. r = PTR_ERR(fence);
  918. else if (fence) {
  919. r = fence_wait_timeout(fence, true, timeout);
  920. fence_put(fence);
  921. } else
  922. r = 1;
  923. amdgpu_ctx_put(ctx);
  924. if (r < 0)
  925. return r;
  926. memset(wait, 0, sizeof(*wait));
  927. wait->out.status = (r == 0);
  928. return 0;
  929. }
  930. /**
  931. * amdgpu_cs_find_bo_va - find bo_va for VM address
  932. *
  933. * @parser: command submission parser context
  934. * @addr: VM address
  935. * @bo: resulting BO of the mapping found
  936. *
  937. * Search the buffer objects in the command submission context for a certain
  938. * virtual memory address. Returns allocation structure when found, NULL
  939. * otherwise.
  940. */
  941. struct amdgpu_bo_va_mapping *
  942. amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
  943. uint64_t addr, struct amdgpu_bo **bo)
  944. {
  945. struct amdgpu_bo_va_mapping *mapping;
  946. unsigned i;
  947. if (!parser->bo_list)
  948. return NULL;
  949. addr /= AMDGPU_GPU_PAGE_SIZE;
  950. for (i = 0; i < parser->bo_list->num_entries; i++) {
  951. struct amdgpu_bo_list_entry *lobj;
  952. lobj = &parser->bo_list->array[i];
  953. if (!lobj->bo_va)
  954. continue;
  955. list_for_each_entry(mapping, &lobj->bo_va->valids, list) {
  956. if (mapping->it.start > addr ||
  957. addr > mapping->it.last)
  958. continue;
  959. *bo = lobj->bo_va->bo;
  960. return mapping;
  961. }
  962. list_for_each_entry(mapping, &lobj->bo_va->invalids, list) {
  963. if (mapping->it.start > addr ||
  964. addr > mapping->it.last)
  965. continue;
  966. *bo = lobj->bo_va->bo;
  967. return mapping;
  968. }
  969. }
  970. return NULL;
  971. }
  972. /**
  973. * amdgpu_cs_sysvm_access_required - make BOs accessible by the system VM
  974. *
  975. * @parser: command submission parser context
  976. *
  977. * Helper for UVD/VCE VM emulation, make sure BOs are accessible by the system VM.
  978. */
  979. int amdgpu_cs_sysvm_access_required(struct amdgpu_cs_parser *parser)
  980. {
  981. unsigned i;
  982. int r;
  983. if (!parser->bo_list)
  984. return 0;
  985. for (i = 0; i < parser->bo_list->num_entries; i++) {
  986. struct amdgpu_bo *bo = parser->bo_list->array[i].robj;
  987. r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem);
  988. if (unlikely(r))
  989. return r;
  990. }
  991. return 0;
  992. }