amdgpu_vm.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846
  1. /*
  2. * Copyright 2008 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. * Copyright 2009 Jerome Glisse.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the 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. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) 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
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors: Dave Airlie
  25. * Alex Deucher
  26. * Jerome Glisse
  27. */
  28. #include <linux/dma-fence-array.h>
  29. #include <drm/drmP.h>
  30. #include <drm/amdgpu_drm.h>
  31. #include "amdgpu.h"
  32. #include "amdgpu_trace.h"
  33. /*
  34. * GPUVM
  35. * GPUVM is similar to the legacy gart on older asics, however
  36. * rather than there being a single global gart table
  37. * for the entire GPU, there are multiple VM page tables active
  38. * at any given time. The VM page tables can contain a mix
  39. * vram pages and system memory pages and system memory pages
  40. * can be mapped as snooped (cached system pages) or unsnooped
  41. * (uncached system pages).
  42. * Each VM has an ID associated with it and there is a page table
  43. * associated with each VMID. When execting a command buffer,
  44. * the kernel tells the the ring what VMID to use for that command
  45. * buffer. VMIDs are allocated dynamically as commands are submitted.
  46. * The userspace drivers maintain their own address space and the kernel
  47. * sets up their pages tables accordingly when they submit their
  48. * command buffers and a VMID is assigned.
  49. * Cayman/Trinity support up to 8 active VMs at any given time;
  50. * SI supports 16.
  51. */
  52. /* Local structure. Encapsulate some VM table update parameters to reduce
  53. * the number of function parameters
  54. */
  55. struct amdgpu_pte_update_params {
  56. /* amdgpu device we do this update for */
  57. struct amdgpu_device *adev;
  58. /* address where to copy page table entries from */
  59. uint64_t src;
  60. /* indirect buffer to fill with commands */
  61. struct amdgpu_ib *ib;
  62. /* Function which actually does the update */
  63. void (*func)(struct amdgpu_pte_update_params *params, uint64_t pe,
  64. uint64_t addr, unsigned count, uint32_t incr,
  65. uint32_t flags);
  66. /* indicate update pt or its shadow */
  67. bool shadow;
  68. };
  69. /* Helper to disable partial resident texture feature from a fence callback */
  70. struct amdgpu_prt_cb {
  71. struct amdgpu_device *adev;
  72. struct dma_fence_cb cb;
  73. };
  74. /**
  75. * amdgpu_vm_num_pde - return the number of page directory entries
  76. *
  77. * @adev: amdgpu_device pointer
  78. *
  79. * Calculate the number of page directory entries.
  80. */
  81. static unsigned amdgpu_vm_num_pdes(struct amdgpu_device *adev)
  82. {
  83. return adev->vm_manager.max_pfn >> amdgpu_vm_block_size;
  84. }
  85. /**
  86. * amdgpu_vm_directory_size - returns the size of the page directory in bytes
  87. *
  88. * @adev: amdgpu_device pointer
  89. *
  90. * Calculate the size of the page directory in bytes.
  91. */
  92. static unsigned amdgpu_vm_directory_size(struct amdgpu_device *adev)
  93. {
  94. return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_pdes(adev) * 8);
  95. }
  96. /**
  97. * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
  98. *
  99. * @vm: vm providing the BOs
  100. * @validated: head of validation list
  101. * @entry: entry to add
  102. *
  103. * Add the page directory to the list of BOs to
  104. * validate for command submission.
  105. */
  106. void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
  107. struct list_head *validated,
  108. struct amdgpu_bo_list_entry *entry)
  109. {
  110. entry->robj = vm->page_directory;
  111. entry->priority = 0;
  112. entry->tv.bo = &vm->page_directory->tbo;
  113. entry->tv.shared = true;
  114. entry->user_pages = NULL;
  115. list_add(&entry->tv.head, validated);
  116. }
  117. /**
  118. * amdgpu_vm_validate_pt_bos - validate the page table BOs
  119. *
  120. * @adev: amdgpu device pointer
  121. * @vm: vm providing the BOs
  122. * @validate: callback to do the validation
  123. * @param: parameter for the validation callback
  124. *
  125. * Validate the page table BOs on command submission if neccessary.
  126. */
  127. int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  128. int (*validate)(void *p, struct amdgpu_bo *bo),
  129. void *param)
  130. {
  131. uint64_t num_evictions;
  132. unsigned i;
  133. int r;
  134. /* We only need to validate the page tables
  135. * if they aren't already valid.
  136. */
  137. num_evictions = atomic64_read(&adev->num_evictions);
  138. if (num_evictions == vm->last_eviction_counter)
  139. return 0;
  140. /* add the vm page table to the list */
  141. for (i = 0; i <= vm->max_pde_used; ++i) {
  142. struct amdgpu_bo *bo = vm->page_tables[i].bo;
  143. if (!bo)
  144. continue;
  145. r = validate(param, bo);
  146. if (r)
  147. return r;
  148. }
  149. return 0;
  150. }
  151. /**
  152. * amdgpu_vm_move_pt_bos_in_lru - move the PT BOs to the LRU tail
  153. *
  154. * @adev: amdgpu device instance
  155. * @vm: vm providing the BOs
  156. *
  157. * Move the PT BOs to the tail of the LRU.
  158. */
  159. void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device *adev,
  160. struct amdgpu_vm *vm)
  161. {
  162. struct ttm_bo_global *glob = adev->mman.bdev.glob;
  163. unsigned i;
  164. spin_lock(&glob->lru_lock);
  165. for (i = 0; i <= vm->max_pde_used; ++i) {
  166. struct amdgpu_bo *bo = vm->page_tables[i].bo;
  167. if (!bo)
  168. continue;
  169. ttm_bo_move_to_lru_tail(&bo->tbo);
  170. }
  171. spin_unlock(&glob->lru_lock);
  172. }
  173. static bool amdgpu_vm_is_gpu_reset(struct amdgpu_device *adev,
  174. struct amdgpu_vm_id *id)
  175. {
  176. return id->current_gpu_reset_count !=
  177. atomic_read(&adev->gpu_reset_counter) ? true : false;
  178. }
  179. /**
  180. * amdgpu_vm_grab_id - allocate the next free VMID
  181. *
  182. * @vm: vm to allocate id for
  183. * @ring: ring we want to submit job to
  184. * @sync: sync object where we add dependencies
  185. * @fence: fence protecting ID from reuse
  186. *
  187. * Allocate an id for the vm, adding fences to the sync obj as necessary.
  188. */
  189. int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
  190. struct amdgpu_sync *sync, struct dma_fence *fence,
  191. struct amdgpu_job *job)
  192. {
  193. struct amdgpu_device *adev = ring->adev;
  194. uint64_t fence_context = adev->fence_context + ring->idx;
  195. struct dma_fence *updates = sync->last_vm_update;
  196. struct amdgpu_vm_id *id, *idle;
  197. struct dma_fence **fences;
  198. unsigned i;
  199. int r = 0;
  200. fences = kmalloc_array(sizeof(void *), adev->vm_manager.num_ids,
  201. GFP_KERNEL);
  202. if (!fences)
  203. return -ENOMEM;
  204. mutex_lock(&adev->vm_manager.lock);
  205. /* Check if we have an idle VMID */
  206. i = 0;
  207. list_for_each_entry(idle, &adev->vm_manager.ids_lru, list) {
  208. fences[i] = amdgpu_sync_peek_fence(&idle->active, ring);
  209. if (!fences[i])
  210. break;
  211. ++i;
  212. }
  213. /* If we can't find a idle VMID to use, wait till one becomes available */
  214. if (&idle->list == &adev->vm_manager.ids_lru) {
  215. u64 fence_context = adev->vm_manager.fence_context + ring->idx;
  216. unsigned seqno = ++adev->vm_manager.seqno[ring->idx];
  217. struct dma_fence_array *array;
  218. unsigned j;
  219. for (j = 0; j < i; ++j)
  220. dma_fence_get(fences[j]);
  221. array = dma_fence_array_create(i, fences, fence_context,
  222. seqno, true);
  223. if (!array) {
  224. for (j = 0; j < i; ++j)
  225. dma_fence_put(fences[j]);
  226. kfree(fences);
  227. r = -ENOMEM;
  228. goto error;
  229. }
  230. r = amdgpu_sync_fence(ring->adev, sync, &array->base);
  231. dma_fence_put(&array->base);
  232. if (r)
  233. goto error;
  234. mutex_unlock(&adev->vm_manager.lock);
  235. return 0;
  236. }
  237. kfree(fences);
  238. job->vm_needs_flush = true;
  239. /* Check if we can use a VMID already assigned to this VM */
  240. i = ring->idx;
  241. do {
  242. struct dma_fence *flushed;
  243. id = vm->ids[i++];
  244. if (i == AMDGPU_MAX_RINGS)
  245. i = 0;
  246. /* Check all the prerequisites to using this VMID */
  247. if (!id)
  248. continue;
  249. if (amdgpu_vm_is_gpu_reset(adev, id))
  250. continue;
  251. if (atomic64_read(&id->owner) != vm->client_id)
  252. continue;
  253. if (job->vm_pd_addr != id->pd_gpu_addr)
  254. continue;
  255. if (!id->last_flush)
  256. continue;
  257. if (id->last_flush->context != fence_context &&
  258. !dma_fence_is_signaled(id->last_flush))
  259. continue;
  260. flushed = id->flushed_updates;
  261. if (updates &&
  262. (!flushed || dma_fence_is_later(updates, flushed)))
  263. continue;
  264. /* Good we can use this VMID. Remember this submission as
  265. * user of the VMID.
  266. */
  267. r = amdgpu_sync_fence(ring->adev, &id->active, fence);
  268. if (r)
  269. goto error;
  270. id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
  271. list_move_tail(&id->list, &adev->vm_manager.ids_lru);
  272. vm->ids[ring->idx] = id;
  273. job->vm_id = id - adev->vm_manager.ids;
  274. job->vm_needs_flush = false;
  275. trace_amdgpu_vm_grab_id(vm, ring->idx, job);
  276. mutex_unlock(&adev->vm_manager.lock);
  277. return 0;
  278. } while (i != ring->idx);
  279. /* Still no ID to use? Then use the idle one found earlier */
  280. id = idle;
  281. /* Remember this submission as user of the VMID */
  282. r = amdgpu_sync_fence(ring->adev, &id->active, fence);
  283. if (r)
  284. goto error;
  285. dma_fence_put(id->first);
  286. id->first = dma_fence_get(fence);
  287. dma_fence_put(id->last_flush);
  288. id->last_flush = NULL;
  289. dma_fence_put(id->flushed_updates);
  290. id->flushed_updates = dma_fence_get(updates);
  291. id->pd_gpu_addr = job->vm_pd_addr;
  292. id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
  293. list_move_tail(&id->list, &adev->vm_manager.ids_lru);
  294. atomic64_set(&id->owner, vm->client_id);
  295. vm->ids[ring->idx] = id;
  296. job->vm_id = id - adev->vm_manager.ids;
  297. trace_amdgpu_vm_grab_id(vm, ring->idx, job);
  298. error:
  299. mutex_unlock(&adev->vm_manager.lock);
  300. return r;
  301. }
  302. static bool amdgpu_vm_ring_has_compute_vm_bug(struct amdgpu_ring *ring)
  303. {
  304. struct amdgpu_device *adev = ring->adev;
  305. const struct amdgpu_ip_block *ip_block;
  306. if (ring->funcs->type != AMDGPU_RING_TYPE_COMPUTE)
  307. /* only compute rings */
  308. return false;
  309. ip_block = amdgpu_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
  310. if (!ip_block)
  311. return false;
  312. if (ip_block->version->major <= 7) {
  313. /* gfx7 has no workaround */
  314. return true;
  315. } else if (ip_block->version->major == 8) {
  316. if (adev->gfx.mec_fw_version >= 673)
  317. /* gfx8 is fixed in MEC firmware 673 */
  318. return false;
  319. else
  320. return true;
  321. }
  322. return false;
  323. }
  324. /**
  325. * amdgpu_vm_flush - hardware flush the vm
  326. *
  327. * @ring: ring to use for flush
  328. * @vm_id: vmid number to use
  329. * @pd_addr: address of the page directory
  330. *
  331. * Emit a VM flush when it is necessary.
  332. */
  333. int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job)
  334. {
  335. struct amdgpu_device *adev = ring->adev;
  336. struct amdgpu_vm_id *id = &adev->vm_manager.ids[job->vm_id];
  337. bool gds_switch_needed = ring->funcs->emit_gds_switch && (
  338. id->gds_base != job->gds_base ||
  339. id->gds_size != job->gds_size ||
  340. id->gws_base != job->gws_base ||
  341. id->gws_size != job->gws_size ||
  342. id->oa_base != job->oa_base ||
  343. id->oa_size != job->oa_size);
  344. int r;
  345. if (ring->funcs->emit_pipeline_sync && (
  346. job->vm_needs_flush || gds_switch_needed ||
  347. amdgpu_vm_ring_has_compute_vm_bug(ring)))
  348. amdgpu_ring_emit_pipeline_sync(ring);
  349. if (ring->funcs->emit_vm_flush && (job->vm_needs_flush ||
  350. amdgpu_vm_is_gpu_reset(adev, id))) {
  351. struct dma_fence *fence;
  352. trace_amdgpu_vm_flush(job->vm_pd_addr, ring->idx, job->vm_id);
  353. amdgpu_ring_emit_vm_flush(ring, job->vm_id, job->vm_pd_addr);
  354. r = amdgpu_fence_emit(ring, &fence);
  355. if (r)
  356. return r;
  357. mutex_lock(&adev->vm_manager.lock);
  358. dma_fence_put(id->last_flush);
  359. id->last_flush = fence;
  360. mutex_unlock(&adev->vm_manager.lock);
  361. }
  362. if (gds_switch_needed) {
  363. id->gds_base = job->gds_base;
  364. id->gds_size = job->gds_size;
  365. id->gws_base = job->gws_base;
  366. id->gws_size = job->gws_size;
  367. id->oa_base = job->oa_base;
  368. id->oa_size = job->oa_size;
  369. amdgpu_ring_emit_gds_switch(ring, job->vm_id,
  370. job->gds_base, job->gds_size,
  371. job->gws_base, job->gws_size,
  372. job->oa_base, job->oa_size);
  373. }
  374. return 0;
  375. }
  376. /**
  377. * amdgpu_vm_reset_id - reset VMID to zero
  378. *
  379. * @adev: amdgpu device structure
  380. * @vm_id: vmid number to use
  381. *
  382. * Reset saved GDW, GWS and OA to force switch on next flush.
  383. */
  384. void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vm_id)
  385. {
  386. struct amdgpu_vm_id *id = &adev->vm_manager.ids[vm_id];
  387. id->gds_base = 0;
  388. id->gds_size = 0;
  389. id->gws_base = 0;
  390. id->gws_size = 0;
  391. id->oa_base = 0;
  392. id->oa_size = 0;
  393. }
  394. /**
  395. * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
  396. *
  397. * @vm: requested vm
  398. * @bo: requested buffer object
  399. *
  400. * Find @bo inside the requested vm.
  401. * Search inside the @bos vm list for the requested vm
  402. * Returns the found bo_va or NULL if none is found
  403. *
  404. * Object has to be reserved!
  405. */
  406. struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
  407. struct amdgpu_bo *bo)
  408. {
  409. struct amdgpu_bo_va *bo_va;
  410. list_for_each_entry(bo_va, &bo->va, bo_list) {
  411. if (bo_va->vm == vm) {
  412. return bo_va;
  413. }
  414. }
  415. return NULL;
  416. }
  417. /**
  418. * amdgpu_vm_do_set_ptes - helper to call the right asic function
  419. *
  420. * @params: see amdgpu_pte_update_params definition
  421. * @pe: addr of the page entry
  422. * @addr: dst addr to write into pe
  423. * @count: number of page entries to update
  424. * @incr: increase next addr by incr bytes
  425. * @flags: hw access flags
  426. *
  427. * Traces the parameters and calls the right asic functions
  428. * to setup the page table using the DMA.
  429. */
  430. static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
  431. uint64_t pe, uint64_t addr,
  432. unsigned count, uint32_t incr,
  433. uint32_t flags)
  434. {
  435. trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
  436. if (count < 3) {
  437. amdgpu_vm_write_pte(params->adev, params->ib, pe,
  438. addr | flags, count, incr);
  439. } else {
  440. amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
  441. count, incr, flags);
  442. }
  443. }
  444. /**
  445. * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
  446. *
  447. * @params: see amdgpu_pte_update_params definition
  448. * @pe: addr of the page entry
  449. * @addr: dst addr to write into pe
  450. * @count: number of page entries to update
  451. * @incr: increase next addr by incr bytes
  452. * @flags: hw access flags
  453. *
  454. * Traces the parameters and calls the DMA function to copy the PTEs.
  455. */
  456. static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
  457. uint64_t pe, uint64_t addr,
  458. unsigned count, uint32_t incr,
  459. uint32_t flags)
  460. {
  461. uint64_t src = (params->src + (addr >> 12) * 8);
  462. trace_amdgpu_vm_copy_ptes(pe, src, count);
  463. amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count);
  464. }
  465. /**
  466. * amdgpu_vm_map_gart - Resolve gart mapping of addr
  467. *
  468. * @pages_addr: optional DMA address to use for lookup
  469. * @addr: the unmapped addr
  470. *
  471. * Look up the physical address of the page that the pte resolves
  472. * to and return the pointer for the page table entry.
  473. */
  474. static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
  475. {
  476. uint64_t result;
  477. /* page table offset */
  478. result = pages_addr[addr >> PAGE_SHIFT];
  479. /* in case cpu page size != gpu page size*/
  480. result |= addr & (~PAGE_MASK);
  481. result &= 0xFFFFFFFFFFFFF000ULL;
  482. return result;
  483. }
  484. /*
  485. * amdgpu_vm_update_pdes - make sure that page directory is valid
  486. *
  487. * @adev: amdgpu_device pointer
  488. * @vm: requested vm
  489. * @start: start of GPU address range
  490. * @end: end of GPU address range
  491. *
  492. * Allocates new page tables if necessary
  493. * and updates the page directory.
  494. * Returns 0 for success, error for failure.
  495. */
  496. int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
  497. struct amdgpu_vm *vm)
  498. {
  499. struct amdgpu_bo *shadow;
  500. struct amdgpu_ring *ring;
  501. uint64_t pd_addr, shadow_addr;
  502. uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
  503. uint64_t last_pde = ~0, last_pt = ~0, last_shadow = ~0;
  504. unsigned count = 0, pt_idx, ndw;
  505. struct amdgpu_job *job;
  506. struct amdgpu_pte_update_params params;
  507. struct dma_fence *fence = NULL;
  508. int r;
  509. ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
  510. shadow = vm->page_directory->shadow;
  511. /* padding, etc. */
  512. ndw = 64;
  513. /* assume the worst case */
  514. ndw += vm->max_pde_used * 6;
  515. pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
  516. if (shadow) {
  517. r = amdgpu_ttm_bind(&shadow->tbo, &shadow->tbo.mem);
  518. if (r)
  519. return r;
  520. shadow_addr = amdgpu_bo_gpu_offset(shadow);
  521. ndw *= 2;
  522. } else {
  523. shadow_addr = 0;
  524. }
  525. r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
  526. if (r)
  527. return r;
  528. memset(&params, 0, sizeof(params));
  529. params.adev = adev;
  530. params.ib = &job->ibs[0];
  531. /* walk over the address space and update the page directory */
  532. for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
  533. struct amdgpu_bo *bo = vm->page_tables[pt_idx].bo;
  534. uint64_t pde, pt;
  535. if (bo == NULL)
  536. continue;
  537. if (bo->shadow) {
  538. struct amdgpu_bo *pt_shadow = bo->shadow;
  539. r = amdgpu_ttm_bind(&pt_shadow->tbo,
  540. &pt_shadow->tbo.mem);
  541. if (r)
  542. return r;
  543. }
  544. pt = amdgpu_bo_gpu_offset(bo);
  545. if (vm->page_tables[pt_idx].addr == pt)
  546. continue;
  547. vm->page_tables[pt_idx].addr = pt;
  548. pde = pd_addr + pt_idx * 8;
  549. if (((last_pde + 8 * count) != pde) ||
  550. ((last_pt + incr * count) != pt) ||
  551. (count == AMDGPU_VM_MAX_UPDATE_SIZE)) {
  552. if (count) {
  553. if (shadow)
  554. amdgpu_vm_do_set_ptes(&params,
  555. last_shadow,
  556. last_pt, count,
  557. incr,
  558. AMDGPU_PTE_VALID);
  559. amdgpu_vm_do_set_ptes(&params, last_pde,
  560. last_pt, count, incr,
  561. AMDGPU_PTE_VALID);
  562. }
  563. count = 1;
  564. last_pde = pde;
  565. last_shadow = shadow_addr + pt_idx * 8;
  566. last_pt = pt;
  567. } else {
  568. ++count;
  569. }
  570. }
  571. if (count) {
  572. if (vm->page_directory->shadow)
  573. amdgpu_vm_do_set_ptes(&params, last_shadow, last_pt,
  574. count, incr, AMDGPU_PTE_VALID);
  575. amdgpu_vm_do_set_ptes(&params, last_pde, last_pt,
  576. count, incr, AMDGPU_PTE_VALID);
  577. }
  578. if (params.ib->length_dw == 0) {
  579. amdgpu_job_free(job);
  580. return 0;
  581. }
  582. amdgpu_ring_pad_ib(ring, params.ib);
  583. amdgpu_sync_resv(adev, &job->sync, vm->page_directory->tbo.resv,
  584. AMDGPU_FENCE_OWNER_VM);
  585. if (shadow)
  586. amdgpu_sync_resv(adev, &job->sync, shadow->tbo.resv,
  587. AMDGPU_FENCE_OWNER_VM);
  588. WARN_ON(params.ib->length_dw > ndw);
  589. r = amdgpu_job_submit(job, ring, &vm->entity,
  590. AMDGPU_FENCE_OWNER_VM, &fence);
  591. if (r)
  592. goto error_free;
  593. amdgpu_bo_fence(vm->page_directory, fence, true);
  594. dma_fence_put(vm->page_directory_fence);
  595. vm->page_directory_fence = dma_fence_get(fence);
  596. dma_fence_put(fence);
  597. return 0;
  598. error_free:
  599. amdgpu_job_free(job);
  600. return r;
  601. }
  602. /**
  603. * amdgpu_vm_update_ptes - make sure that page tables are valid
  604. *
  605. * @params: see amdgpu_pte_update_params definition
  606. * @vm: requested vm
  607. * @start: start of GPU address range
  608. * @end: end of GPU address range
  609. * @dst: destination address to map to, the next dst inside the function
  610. * @flags: mapping flags
  611. *
  612. * Update the page tables in the range @start - @end.
  613. */
  614. static void amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
  615. struct amdgpu_vm *vm,
  616. uint64_t start, uint64_t end,
  617. uint64_t dst, uint32_t flags)
  618. {
  619. const uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
  620. uint64_t cur_pe_start, cur_nptes, cur_dst;
  621. uint64_t addr; /* next GPU address to be updated */
  622. uint64_t pt_idx;
  623. struct amdgpu_bo *pt;
  624. unsigned nptes; /* next number of ptes to be updated */
  625. uint64_t next_pe_start;
  626. /* initialize the variables */
  627. addr = start;
  628. pt_idx = addr >> amdgpu_vm_block_size;
  629. pt = vm->page_tables[pt_idx].bo;
  630. if (params->shadow) {
  631. if (!pt->shadow)
  632. return;
  633. pt = pt->shadow;
  634. }
  635. if ((addr & ~mask) == (end & ~mask))
  636. nptes = end - addr;
  637. else
  638. nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
  639. cur_pe_start = amdgpu_bo_gpu_offset(pt);
  640. cur_pe_start += (addr & mask) * 8;
  641. cur_nptes = nptes;
  642. cur_dst = dst;
  643. /* for next ptb*/
  644. addr += nptes;
  645. dst += nptes * AMDGPU_GPU_PAGE_SIZE;
  646. /* walk over the address space and update the page tables */
  647. while (addr < end) {
  648. pt_idx = addr >> amdgpu_vm_block_size;
  649. pt = vm->page_tables[pt_idx].bo;
  650. if (params->shadow) {
  651. if (!pt->shadow)
  652. return;
  653. pt = pt->shadow;
  654. }
  655. if ((addr & ~mask) == (end & ~mask))
  656. nptes = end - addr;
  657. else
  658. nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
  659. next_pe_start = amdgpu_bo_gpu_offset(pt);
  660. next_pe_start += (addr & mask) * 8;
  661. if ((cur_pe_start + 8 * cur_nptes) == next_pe_start &&
  662. ((cur_nptes + nptes) <= AMDGPU_VM_MAX_UPDATE_SIZE)) {
  663. /* The next ptb is consecutive to current ptb.
  664. * Don't call the update function now.
  665. * Will update two ptbs together in future.
  666. */
  667. cur_nptes += nptes;
  668. } else {
  669. params->func(params, cur_pe_start, cur_dst, cur_nptes,
  670. AMDGPU_GPU_PAGE_SIZE, flags);
  671. cur_pe_start = next_pe_start;
  672. cur_nptes = nptes;
  673. cur_dst = dst;
  674. }
  675. /* for next ptb*/
  676. addr += nptes;
  677. dst += nptes * AMDGPU_GPU_PAGE_SIZE;
  678. }
  679. params->func(params, cur_pe_start, cur_dst, cur_nptes,
  680. AMDGPU_GPU_PAGE_SIZE, flags);
  681. }
  682. /*
  683. * amdgpu_vm_frag_ptes - add fragment information to PTEs
  684. *
  685. * @params: see amdgpu_pte_update_params definition
  686. * @vm: requested vm
  687. * @start: first PTE to handle
  688. * @end: last PTE to handle
  689. * @dst: addr those PTEs should point to
  690. * @flags: hw mapping flags
  691. */
  692. static void amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
  693. struct amdgpu_vm *vm,
  694. uint64_t start, uint64_t end,
  695. uint64_t dst, uint32_t flags)
  696. {
  697. /**
  698. * The MC L1 TLB supports variable sized pages, based on a fragment
  699. * field in the PTE. When this field is set to a non-zero value, page
  700. * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
  701. * flags are considered valid for all PTEs within the fragment range
  702. * and corresponding mappings are assumed to be physically contiguous.
  703. *
  704. * The L1 TLB can store a single PTE for the whole fragment,
  705. * significantly increasing the space available for translation
  706. * caching. This leads to large improvements in throughput when the
  707. * TLB is under pressure.
  708. *
  709. * The L2 TLB distributes small and large fragments into two
  710. * asymmetric partitions. The large fragment cache is significantly
  711. * larger. Thus, we try to use large fragments wherever possible.
  712. * Userspace can support this by aligning virtual base address and
  713. * allocation size to the fragment size.
  714. */
  715. /* SI and newer are optimized for 64KB */
  716. uint64_t frag_flags = AMDGPU_PTE_FRAG(AMDGPU_LOG2_PAGES_PER_FRAG);
  717. uint64_t frag_align = 1 << AMDGPU_LOG2_PAGES_PER_FRAG;
  718. uint64_t frag_start = ALIGN(start, frag_align);
  719. uint64_t frag_end = end & ~(frag_align - 1);
  720. /* system pages are non continuously */
  721. if (params->src || !(flags & AMDGPU_PTE_VALID) ||
  722. (frag_start >= frag_end)) {
  723. amdgpu_vm_update_ptes(params, vm, start, end, dst, flags);
  724. return;
  725. }
  726. /* handle the 4K area at the beginning */
  727. if (start != frag_start) {
  728. amdgpu_vm_update_ptes(params, vm, start, frag_start,
  729. dst, flags);
  730. dst += (frag_start - start) * AMDGPU_GPU_PAGE_SIZE;
  731. }
  732. /* handle the area in the middle */
  733. amdgpu_vm_update_ptes(params, vm, frag_start, frag_end, dst,
  734. flags | frag_flags);
  735. /* handle the 4K area at the end */
  736. if (frag_end != end) {
  737. dst += (frag_end - frag_start) * AMDGPU_GPU_PAGE_SIZE;
  738. amdgpu_vm_update_ptes(params, vm, frag_end, end, dst, flags);
  739. }
  740. }
  741. /**
  742. * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
  743. *
  744. * @adev: amdgpu_device pointer
  745. * @exclusive: fence we need to sync to
  746. * @src: address where to copy page table entries from
  747. * @pages_addr: DMA addresses to use for mapping
  748. * @vm: requested vm
  749. * @start: start of mapped range
  750. * @last: last mapped entry
  751. * @flags: flags for the entries
  752. * @addr: addr to set the area to
  753. * @fence: optional resulting fence
  754. *
  755. * Fill in the page table entries between @start and @last.
  756. * Returns 0 for success, -EINVAL for failure.
  757. */
  758. static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
  759. struct dma_fence *exclusive,
  760. uint64_t src,
  761. dma_addr_t *pages_addr,
  762. struct amdgpu_vm *vm,
  763. uint64_t start, uint64_t last,
  764. uint32_t flags, uint64_t addr,
  765. struct dma_fence **fence)
  766. {
  767. struct amdgpu_ring *ring;
  768. void *owner = AMDGPU_FENCE_OWNER_VM;
  769. unsigned nptes, ncmds, ndw;
  770. struct amdgpu_job *job;
  771. struct amdgpu_pte_update_params params;
  772. struct dma_fence *f = NULL;
  773. int r;
  774. memset(&params, 0, sizeof(params));
  775. params.adev = adev;
  776. params.src = src;
  777. ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
  778. memset(&params, 0, sizeof(params));
  779. params.adev = adev;
  780. params.src = src;
  781. /* sync to everything on unmapping */
  782. if (!(flags & AMDGPU_PTE_VALID))
  783. owner = AMDGPU_FENCE_OWNER_UNDEFINED;
  784. nptes = last - start + 1;
  785. /*
  786. * reserve space for one command every (1 << BLOCK_SIZE)
  787. * entries or 2k dwords (whatever is smaller)
  788. */
  789. ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
  790. /* padding, etc. */
  791. ndw = 64;
  792. if (src) {
  793. /* only copy commands needed */
  794. ndw += ncmds * 7;
  795. params.func = amdgpu_vm_do_copy_ptes;
  796. } else if (pages_addr) {
  797. /* copy commands needed */
  798. ndw += ncmds * 7;
  799. /* and also PTEs */
  800. ndw += nptes * 2;
  801. params.func = amdgpu_vm_do_copy_ptes;
  802. } else {
  803. /* set page commands needed */
  804. ndw += ncmds * 10;
  805. /* two extra commands for begin/end of fragment */
  806. ndw += 2 * 10;
  807. params.func = amdgpu_vm_do_set_ptes;
  808. }
  809. r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
  810. if (r)
  811. return r;
  812. params.ib = &job->ibs[0];
  813. if (!src && pages_addr) {
  814. uint64_t *pte;
  815. unsigned i;
  816. /* Put the PTEs at the end of the IB. */
  817. i = ndw - nptes * 2;
  818. pte= (uint64_t *)&(job->ibs->ptr[i]);
  819. params.src = job->ibs->gpu_addr + i * 4;
  820. for (i = 0; i < nptes; ++i) {
  821. pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
  822. AMDGPU_GPU_PAGE_SIZE);
  823. pte[i] |= flags;
  824. }
  825. addr = 0;
  826. }
  827. r = amdgpu_sync_fence(adev, &job->sync, exclusive);
  828. if (r)
  829. goto error_free;
  830. r = amdgpu_sync_resv(adev, &job->sync, vm->page_directory->tbo.resv,
  831. owner);
  832. if (r)
  833. goto error_free;
  834. r = reservation_object_reserve_shared(vm->page_directory->tbo.resv);
  835. if (r)
  836. goto error_free;
  837. params.shadow = true;
  838. amdgpu_vm_frag_ptes(&params, vm, start, last + 1, addr, flags);
  839. params.shadow = false;
  840. amdgpu_vm_frag_ptes(&params, vm, start, last + 1, addr, flags);
  841. amdgpu_ring_pad_ib(ring, params.ib);
  842. WARN_ON(params.ib->length_dw > ndw);
  843. r = amdgpu_job_submit(job, ring, &vm->entity,
  844. AMDGPU_FENCE_OWNER_VM, &f);
  845. if (r)
  846. goto error_free;
  847. amdgpu_bo_fence(vm->page_directory, f, true);
  848. dma_fence_put(*fence);
  849. *fence = f;
  850. return 0;
  851. error_free:
  852. amdgpu_job_free(job);
  853. return r;
  854. }
  855. /**
  856. * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
  857. *
  858. * @adev: amdgpu_device pointer
  859. * @exclusive: fence we need to sync to
  860. * @gtt_flags: flags as they are used for GTT
  861. * @pages_addr: DMA addresses to use for mapping
  862. * @vm: requested vm
  863. * @mapping: mapped range and flags to use for the update
  864. * @flags: HW flags for the mapping
  865. * @nodes: array of drm_mm_nodes with the MC addresses
  866. * @fence: optional resulting fence
  867. *
  868. * Split the mapping into smaller chunks so that each update fits
  869. * into a SDMA IB.
  870. * Returns 0 for success, -EINVAL for failure.
  871. */
  872. static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
  873. struct dma_fence *exclusive,
  874. uint32_t gtt_flags,
  875. dma_addr_t *pages_addr,
  876. struct amdgpu_vm *vm,
  877. struct amdgpu_bo_va_mapping *mapping,
  878. uint32_t flags,
  879. struct drm_mm_node *nodes,
  880. struct dma_fence **fence)
  881. {
  882. uint64_t pfn, src = 0, start = mapping->it.start;
  883. int r;
  884. /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
  885. * but in case of something, we filter the flags in first place
  886. */
  887. if (!(mapping->flags & AMDGPU_PTE_READABLE))
  888. flags &= ~AMDGPU_PTE_READABLE;
  889. if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
  890. flags &= ~AMDGPU_PTE_WRITEABLE;
  891. trace_amdgpu_vm_bo_update(mapping);
  892. pfn = mapping->offset >> PAGE_SHIFT;
  893. if (nodes) {
  894. while (pfn >= nodes->size) {
  895. pfn -= nodes->size;
  896. ++nodes;
  897. }
  898. }
  899. do {
  900. uint64_t max_entries;
  901. uint64_t addr, last;
  902. if (nodes) {
  903. addr = nodes->start << PAGE_SHIFT;
  904. max_entries = (nodes->size - pfn) *
  905. (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
  906. } else {
  907. addr = 0;
  908. max_entries = S64_MAX;
  909. }
  910. if (pages_addr) {
  911. if (flags == gtt_flags)
  912. src = adev->gart.table_addr +
  913. (addr >> AMDGPU_GPU_PAGE_SHIFT) * 8;
  914. else
  915. max_entries = min(max_entries, 16ull * 1024ull);
  916. addr = 0;
  917. } else if (flags & AMDGPU_PTE_VALID) {
  918. addr += adev->vm_manager.vram_base_offset;
  919. }
  920. addr += pfn << PAGE_SHIFT;
  921. last = min((uint64_t)mapping->it.last, start + max_entries - 1);
  922. r = amdgpu_vm_bo_update_mapping(adev, exclusive,
  923. src, pages_addr, vm,
  924. start, last, flags, addr,
  925. fence);
  926. if (r)
  927. return r;
  928. pfn += last - start + 1;
  929. if (nodes && nodes->size == pfn) {
  930. pfn = 0;
  931. ++nodes;
  932. }
  933. start = last + 1;
  934. } while (unlikely(start != mapping->it.last + 1));
  935. return 0;
  936. }
  937. /**
  938. * amdgpu_vm_bo_update - update all BO mappings in the vm page table
  939. *
  940. * @adev: amdgpu_device pointer
  941. * @bo_va: requested BO and VM object
  942. * @clear: if true clear the entries
  943. *
  944. * Fill in the page table entries for @bo_va.
  945. * Returns 0 for success, -EINVAL for failure.
  946. */
  947. int amdgpu_vm_bo_update(struct amdgpu_device *adev,
  948. struct amdgpu_bo_va *bo_va,
  949. bool clear)
  950. {
  951. struct amdgpu_vm *vm = bo_va->vm;
  952. struct amdgpu_bo_va_mapping *mapping;
  953. dma_addr_t *pages_addr = NULL;
  954. uint32_t gtt_flags, flags;
  955. struct ttm_mem_reg *mem;
  956. struct drm_mm_node *nodes;
  957. struct dma_fence *exclusive;
  958. int r;
  959. if (clear || !bo_va->bo) {
  960. mem = NULL;
  961. nodes = NULL;
  962. exclusive = NULL;
  963. } else {
  964. struct ttm_dma_tt *ttm;
  965. mem = &bo_va->bo->tbo.mem;
  966. nodes = mem->mm_node;
  967. if (mem->mem_type == TTM_PL_TT) {
  968. ttm = container_of(bo_va->bo->tbo.ttm, struct
  969. ttm_dma_tt, ttm);
  970. pages_addr = ttm->dma_address;
  971. }
  972. exclusive = reservation_object_get_excl(bo_va->bo->tbo.resv);
  973. }
  974. if (bo_va->bo) {
  975. flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
  976. gtt_flags = (amdgpu_ttm_is_bound(bo_va->bo->tbo.ttm) &&
  977. adev == amdgpu_ttm_adev(bo_va->bo->tbo.bdev)) ?
  978. flags : 0;
  979. } else {
  980. flags = 0x0;
  981. gtt_flags = ~0x0;
  982. }
  983. spin_lock(&vm->status_lock);
  984. if (!list_empty(&bo_va->vm_status))
  985. list_splice_init(&bo_va->valids, &bo_va->invalids);
  986. spin_unlock(&vm->status_lock);
  987. list_for_each_entry(mapping, &bo_va->invalids, list) {
  988. r = amdgpu_vm_bo_split_mapping(adev, exclusive,
  989. gtt_flags, pages_addr, vm,
  990. mapping, flags, nodes,
  991. &bo_va->last_pt_update);
  992. if (r)
  993. return r;
  994. }
  995. if (trace_amdgpu_vm_bo_mapping_enabled()) {
  996. list_for_each_entry(mapping, &bo_va->valids, list)
  997. trace_amdgpu_vm_bo_mapping(mapping);
  998. list_for_each_entry(mapping, &bo_va->invalids, list)
  999. trace_amdgpu_vm_bo_mapping(mapping);
  1000. }
  1001. spin_lock(&vm->status_lock);
  1002. list_splice_init(&bo_va->invalids, &bo_va->valids);
  1003. list_del_init(&bo_va->vm_status);
  1004. if (clear)
  1005. list_add(&bo_va->vm_status, &vm->cleared);
  1006. spin_unlock(&vm->status_lock);
  1007. return 0;
  1008. }
  1009. /**
  1010. * amdgpu_vm_update_prt_state - update the global PRT state
  1011. */
  1012. static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
  1013. {
  1014. unsigned long flags;
  1015. bool enable;
  1016. spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
  1017. enable = !!atomic_read(&adev->vm_manager.num_prt_users);
  1018. adev->gart.gart_funcs->set_prt(adev, enable);
  1019. spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
  1020. }
  1021. /**
  1022. * amdgpu_vm_prt_put - add a PRT user
  1023. */
  1024. static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
  1025. {
  1026. if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
  1027. amdgpu_vm_update_prt_state(adev);
  1028. }
  1029. /**
  1030. * amdgpu_vm_prt_put - drop a PRT user
  1031. */
  1032. static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
  1033. {
  1034. if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
  1035. amdgpu_vm_update_prt_state(adev);
  1036. }
  1037. /**
  1038. * amdgpu_vm_prt_cb - callback for updating the PRT status
  1039. */
  1040. static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
  1041. {
  1042. struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
  1043. amdgpu_vm_prt_put(cb->adev);
  1044. kfree(cb);
  1045. }
  1046. /**
  1047. * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
  1048. */
  1049. static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
  1050. struct dma_fence *fence)
  1051. {
  1052. struct amdgpu_prt_cb *cb = kmalloc(sizeof(struct amdgpu_prt_cb),
  1053. GFP_KERNEL);
  1054. if (!cb) {
  1055. /* Last resort when we are OOM */
  1056. if (fence)
  1057. dma_fence_wait(fence, false);
  1058. amdgpu_vm_prt_put(cb->adev);
  1059. } else {
  1060. cb->adev = adev;
  1061. if (!fence || dma_fence_add_callback(fence, &cb->cb,
  1062. amdgpu_vm_prt_cb))
  1063. amdgpu_vm_prt_cb(fence, &cb->cb);
  1064. }
  1065. }
  1066. /**
  1067. * amdgpu_vm_free_mapping - free a mapping
  1068. *
  1069. * @adev: amdgpu_device pointer
  1070. * @vm: requested vm
  1071. * @mapping: mapping to be freed
  1072. * @fence: fence of the unmap operation
  1073. *
  1074. * Free a mapping and make sure we decrease the PRT usage count if applicable.
  1075. */
  1076. static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
  1077. struct amdgpu_vm *vm,
  1078. struct amdgpu_bo_va_mapping *mapping,
  1079. struct dma_fence *fence)
  1080. {
  1081. if (mapping->flags & AMDGPU_PTE_PRT)
  1082. amdgpu_vm_add_prt_cb(adev, fence);
  1083. kfree(mapping);
  1084. }
  1085. /**
  1086. * amdgpu_vm_prt_fini - finish all prt mappings
  1087. *
  1088. * @adev: amdgpu_device pointer
  1089. * @vm: requested vm
  1090. *
  1091. * Register a cleanup callback to disable PRT support after VM dies.
  1092. */
  1093. static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  1094. {
  1095. struct reservation_object *resv = vm->page_directory->tbo.resv;
  1096. struct dma_fence *excl, **shared;
  1097. unsigned i, shared_count;
  1098. int r;
  1099. r = reservation_object_get_fences_rcu(resv, &excl,
  1100. &shared_count, &shared);
  1101. if (r) {
  1102. /* Not enough memory to grab the fence list, as last resort
  1103. * block for all the fences to complete.
  1104. */
  1105. reservation_object_wait_timeout_rcu(resv, true, false,
  1106. MAX_SCHEDULE_TIMEOUT);
  1107. return;
  1108. }
  1109. /* Add a callback for each fence in the reservation object */
  1110. amdgpu_vm_prt_get(adev);
  1111. amdgpu_vm_add_prt_cb(adev, excl);
  1112. for (i = 0; i < shared_count; ++i) {
  1113. amdgpu_vm_prt_get(adev);
  1114. amdgpu_vm_add_prt_cb(adev, shared[i]);
  1115. }
  1116. kfree(shared);
  1117. }
  1118. /**
  1119. * amdgpu_vm_clear_freed - clear freed BOs in the PT
  1120. *
  1121. * @adev: amdgpu_device pointer
  1122. * @vm: requested vm
  1123. *
  1124. * Make sure all freed BOs are cleared in the PT.
  1125. * Returns 0 for success.
  1126. *
  1127. * PTs have to be reserved and mutex must be locked!
  1128. */
  1129. int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
  1130. struct amdgpu_vm *vm)
  1131. {
  1132. struct amdgpu_bo_va_mapping *mapping;
  1133. struct dma_fence *fence = NULL;
  1134. int r;
  1135. while (!list_empty(&vm->freed)) {
  1136. mapping = list_first_entry(&vm->freed,
  1137. struct amdgpu_bo_va_mapping, list);
  1138. list_del(&mapping->list);
  1139. r = amdgpu_vm_bo_split_mapping(adev, NULL, 0, NULL, vm, mapping,
  1140. 0, 0, &fence);
  1141. amdgpu_vm_free_mapping(adev, vm, mapping, fence);
  1142. if (r) {
  1143. dma_fence_put(fence);
  1144. return r;
  1145. }
  1146. }
  1147. dma_fence_put(fence);
  1148. return 0;
  1149. }
  1150. /**
  1151. * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
  1152. *
  1153. * @adev: amdgpu_device pointer
  1154. * @vm: requested vm
  1155. *
  1156. * Make sure all invalidated BOs are cleared in the PT.
  1157. * Returns 0 for success.
  1158. *
  1159. * PTs have to be reserved and mutex must be locked!
  1160. */
  1161. int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
  1162. struct amdgpu_vm *vm, struct amdgpu_sync *sync)
  1163. {
  1164. struct amdgpu_bo_va *bo_va = NULL;
  1165. int r = 0;
  1166. spin_lock(&vm->status_lock);
  1167. while (!list_empty(&vm->invalidated)) {
  1168. bo_va = list_first_entry(&vm->invalidated,
  1169. struct amdgpu_bo_va, vm_status);
  1170. spin_unlock(&vm->status_lock);
  1171. r = amdgpu_vm_bo_update(adev, bo_va, true);
  1172. if (r)
  1173. return r;
  1174. spin_lock(&vm->status_lock);
  1175. }
  1176. spin_unlock(&vm->status_lock);
  1177. if (bo_va)
  1178. r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
  1179. return r;
  1180. }
  1181. /**
  1182. * amdgpu_vm_bo_add - add a bo to a specific vm
  1183. *
  1184. * @adev: amdgpu_device pointer
  1185. * @vm: requested vm
  1186. * @bo: amdgpu buffer object
  1187. *
  1188. * Add @bo into the requested vm.
  1189. * Add @bo to the list of bos associated with the vm
  1190. * Returns newly added bo_va or NULL for failure
  1191. *
  1192. * Object has to be reserved!
  1193. */
  1194. struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
  1195. struct amdgpu_vm *vm,
  1196. struct amdgpu_bo *bo)
  1197. {
  1198. struct amdgpu_bo_va *bo_va;
  1199. bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
  1200. if (bo_va == NULL) {
  1201. return NULL;
  1202. }
  1203. bo_va->vm = vm;
  1204. bo_va->bo = bo;
  1205. bo_va->ref_count = 1;
  1206. INIT_LIST_HEAD(&bo_va->bo_list);
  1207. INIT_LIST_HEAD(&bo_va->valids);
  1208. INIT_LIST_HEAD(&bo_va->invalids);
  1209. INIT_LIST_HEAD(&bo_va->vm_status);
  1210. if (bo)
  1211. list_add_tail(&bo_va->bo_list, &bo->va);
  1212. return bo_va;
  1213. }
  1214. /**
  1215. * amdgpu_vm_bo_map - map bo inside a vm
  1216. *
  1217. * @adev: amdgpu_device pointer
  1218. * @bo_va: bo_va to store the address
  1219. * @saddr: where to map the BO
  1220. * @offset: requested offset in the BO
  1221. * @flags: attributes of pages (read/write/valid/etc.)
  1222. *
  1223. * Add a mapping of the BO at the specefied addr into the VM.
  1224. * Returns 0 for success, error for failure.
  1225. *
  1226. * Object has to be reserved and unreserved outside!
  1227. */
  1228. int amdgpu_vm_bo_map(struct amdgpu_device *adev,
  1229. struct amdgpu_bo_va *bo_va,
  1230. uint64_t saddr, uint64_t offset,
  1231. uint64_t size, uint64_t flags)
  1232. {
  1233. struct amdgpu_bo_va_mapping *mapping;
  1234. struct amdgpu_vm *vm = bo_va->vm;
  1235. struct interval_tree_node *it;
  1236. unsigned last_pfn, pt_idx;
  1237. uint64_t eaddr;
  1238. int r;
  1239. /* validate the parameters */
  1240. if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
  1241. size == 0 || size & AMDGPU_GPU_PAGE_MASK)
  1242. return -EINVAL;
  1243. if (flags & AMDGPU_PTE_PRT) {
  1244. /* Check if we have PRT hardware support */
  1245. if (!adev->gart.gart_funcs->set_prt)
  1246. return -EINVAL;
  1247. amdgpu_vm_prt_get(adev);
  1248. }
  1249. /* make sure object fit at this offset */
  1250. eaddr = saddr + size - 1;
  1251. if (saddr >= eaddr ||
  1252. (bo_va->bo && offset + size > amdgpu_bo_size(bo_va->bo)))
  1253. return -EINVAL;
  1254. last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
  1255. if (last_pfn >= adev->vm_manager.max_pfn) {
  1256. dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n",
  1257. last_pfn, adev->vm_manager.max_pfn);
  1258. return -EINVAL;
  1259. }
  1260. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1261. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  1262. it = interval_tree_iter_first(&vm->va, saddr, eaddr);
  1263. if (it) {
  1264. struct amdgpu_bo_va_mapping *tmp;
  1265. tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
  1266. /* bo and tmp overlap, invalid addr */
  1267. dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
  1268. "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
  1269. tmp->it.start, tmp->it.last + 1);
  1270. r = -EINVAL;
  1271. goto error;
  1272. }
  1273. mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
  1274. if (!mapping) {
  1275. r = -ENOMEM;
  1276. goto error;
  1277. }
  1278. INIT_LIST_HEAD(&mapping->list);
  1279. mapping->it.start = saddr;
  1280. mapping->it.last = eaddr;
  1281. mapping->offset = offset;
  1282. mapping->flags = flags;
  1283. list_add(&mapping->list, &bo_va->invalids);
  1284. interval_tree_insert(&mapping->it, &vm->va);
  1285. /* Make sure the page tables are allocated */
  1286. saddr >>= amdgpu_vm_block_size;
  1287. eaddr >>= amdgpu_vm_block_size;
  1288. BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
  1289. if (eaddr > vm->max_pde_used)
  1290. vm->max_pde_used = eaddr;
  1291. /* walk over the address space and allocate the page tables */
  1292. for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
  1293. struct reservation_object *resv = vm->page_directory->tbo.resv;
  1294. struct amdgpu_bo *pt;
  1295. if (vm->page_tables[pt_idx].bo)
  1296. continue;
  1297. r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
  1298. AMDGPU_GPU_PAGE_SIZE, true,
  1299. AMDGPU_GEM_DOMAIN_VRAM,
  1300. AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
  1301. AMDGPU_GEM_CREATE_SHADOW |
  1302. AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
  1303. AMDGPU_GEM_CREATE_VRAM_CLEARED,
  1304. NULL, resv, &pt);
  1305. if (r)
  1306. goto error_free;
  1307. /* Keep a reference to the page table to avoid freeing
  1308. * them up in the wrong order.
  1309. */
  1310. pt->parent = amdgpu_bo_ref(vm->page_directory);
  1311. vm->page_tables[pt_idx].bo = pt;
  1312. vm->page_tables[pt_idx].addr = 0;
  1313. }
  1314. return 0;
  1315. error_free:
  1316. list_del(&mapping->list);
  1317. interval_tree_remove(&mapping->it, &vm->va);
  1318. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  1319. amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
  1320. error:
  1321. return r;
  1322. }
  1323. /**
  1324. * amdgpu_vm_bo_unmap - remove bo mapping from vm
  1325. *
  1326. * @adev: amdgpu_device pointer
  1327. * @bo_va: bo_va to remove the address from
  1328. * @saddr: where to the BO is mapped
  1329. *
  1330. * Remove a mapping of the BO at the specefied addr from the VM.
  1331. * Returns 0 for success, error for failure.
  1332. *
  1333. * Object has to be reserved and unreserved outside!
  1334. */
  1335. int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
  1336. struct amdgpu_bo_va *bo_va,
  1337. uint64_t saddr)
  1338. {
  1339. struct amdgpu_bo_va_mapping *mapping;
  1340. struct amdgpu_vm *vm = bo_va->vm;
  1341. bool valid = true;
  1342. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1343. list_for_each_entry(mapping, &bo_va->valids, list) {
  1344. if (mapping->it.start == saddr)
  1345. break;
  1346. }
  1347. if (&mapping->list == &bo_va->valids) {
  1348. valid = false;
  1349. list_for_each_entry(mapping, &bo_va->invalids, list) {
  1350. if (mapping->it.start == saddr)
  1351. break;
  1352. }
  1353. if (&mapping->list == &bo_va->invalids)
  1354. return -ENOENT;
  1355. }
  1356. list_del(&mapping->list);
  1357. interval_tree_remove(&mapping->it, &vm->va);
  1358. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  1359. if (valid)
  1360. list_add(&mapping->list, &vm->freed);
  1361. else
  1362. amdgpu_vm_free_mapping(adev, vm, mapping,
  1363. bo_va->last_pt_update);
  1364. return 0;
  1365. }
  1366. /**
  1367. * amdgpu_vm_bo_rmv - remove a bo to a specific vm
  1368. *
  1369. * @adev: amdgpu_device pointer
  1370. * @bo_va: requested bo_va
  1371. *
  1372. * Remove @bo_va->bo from the requested vm.
  1373. *
  1374. * Object have to be reserved!
  1375. */
  1376. void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
  1377. struct amdgpu_bo_va *bo_va)
  1378. {
  1379. struct amdgpu_bo_va_mapping *mapping, *next;
  1380. struct amdgpu_vm *vm = bo_va->vm;
  1381. list_del(&bo_va->bo_list);
  1382. spin_lock(&vm->status_lock);
  1383. list_del(&bo_va->vm_status);
  1384. spin_unlock(&vm->status_lock);
  1385. list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
  1386. list_del(&mapping->list);
  1387. interval_tree_remove(&mapping->it, &vm->va);
  1388. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  1389. list_add(&mapping->list, &vm->freed);
  1390. }
  1391. list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
  1392. list_del(&mapping->list);
  1393. interval_tree_remove(&mapping->it, &vm->va);
  1394. amdgpu_vm_free_mapping(adev, vm, mapping,
  1395. bo_va->last_pt_update);
  1396. }
  1397. dma_fence_put(bo_va->last_pt_update);
  1398. kfree(bo_va);
  1399. }
  1400. /**
  1401. * amdgpu_vm_bo_invalidate - mark the bo as invalid
  1402. *
  1403. * @adev: amdgpu_device pointer
  1404. * @vm: requested vm
  1405. * @bo: amdgpu buffer object
  1406. *
  1407. * Mark @bo as invalid.
  1408. */
  1409. void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
  1410. struct amdgpu_bo *bo)
  1411. {
  1412. struct amdgpu_bo_va *bo_va;
  1413. list_for_each_entry(bo_va, &bo->va, bo_list) {
  1414. spin_lock(&bo_va->vm->status_lock);
  1415. if (list_empty(&bo_va->vm_status))
  1416. list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
  1417. spin_unlock(&bo_va->vm->status_lock);
  1418. }
  1419. }
  1420. /**
  1421. * amdgpu_vm_init - initialize a vm instance
  1422. *
  1423. * @adev: amdgpu_device pointer
  1424. * @vm: requested vm
  1425. *
  1426. * Init @vm fields.
  1427. */
  1428. int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  1429. {
  1430. const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
  1431. AMDGPU_VM_PTE_COUNT * 8);
  1432. unsigned pd_size, pd_entries;
  1433. unsigned ring_instance;
  1434. struct amdgpu_ring *ring;
  1435. struct amd_sched_rq *rq;
  1436. int i, r;
  1437. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  1438. vm->ids[i] = NULL;
  1439. vm->va = RB_ROOT;
  1440. vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
  1441. spin_lock_init(&vm->status_lock);
  1442. INIT_LIST_HEAD(&vm->invalidated);
  1443. INIT_LIST_HEAD(&vm->cleared);
  1444. INIT_LIST_HEAD(&vm->freed);
  1445. pd_size = amdgpu_vm_directory_size(adev);
  1446. pd_entries = amdgpu_vm_num_pdes(adev);
  1447. /* allocate page table array */
  1448. vm->page_tables = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt));
  1449. if (vm->page_tables == NULL) {
  1450. DRM_ERROR("Cannot allocate memory for page table array\n");
  1451. return -ENOMEM;
  1452. }
  1453. /* create scheduler entity for page table updates */
  1454. ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
  1455. ring_instance %= adev->vm_manager.vm_pte_num_rings;
  1456. ring = adev->vm_manager.vm_pte_rings[ring_instance];
  1457. rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
  1458. r = amd_sched_entity_init(&ring->sched, &vm->entity,
  1459. rq, amdgpu_sched_jobs);
  1460. if (r)
  1461. goto err;
  1462. vm->page_directory_fence = NULL;
  1463. r = amdgpu_bo_create(adev, pd_size, align, true,
  1464. AMDGPU_GEM_DOMAIN_VRAM,
  1465. AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
  1466. AMDGPU_GEM_CREATE_SHADOW |
  1467. AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
  1468. AMDGPU_GEM_CREATE_VRAM_CLEARED,
  1469. NULL, NULL, &vm->page_directory);
  1470. if (r)
  1471. goto error_free_sched_entity;
  1472. r = amdgpu_bo_reserve(vm->page_directory, false);
  1473. if (r)
  1474. goto error_free_page_directory;
  1475. vm->last_eviction_counter = atomic64_read(&adev->num_evictions);
  1476. amdgpu_bo_unreserve(vm->page_directory);
  1477. return 0;
  1478. error_free_page_directory:
  1479. amdgpu_bo_unref(&vm->page_directory->shadow);
  1480. amdgpu_bo_unref(&vm->page_directory);
  1481. vm->page_directory = NULL;
  1482. error_free_sched_entity:
  1483. amd_sched_entity_fini(&ring->sched, &vm->entity);
  1484. err:
  1485. drm_free_large(vm->page_tables);
  1486. return r;
  1487. }
  1488. /**
  1489. * amdgpu_vm_fini - tear down a vm instance
  1490. *
  1491. * @adev: amdgpu_device pointer
  1492. * @vm: requested vm
  1493. *
  1494. * Tear down @vm.
  1495. * Unbind the VM and remove all bos from the vm bo list
  1496. */
  1497. void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  1498. {
  1499. struct amdgpu_bo_va_mapping *mapping, *tmp;
  1500. bool prt_fini_called = false;
  1501. int i;
  1502. amd_sched_entity_fini(vm->entity.sched, &vm->entity);
  1503. if (!RB_EMPTY_ROOT(&vm->va)) {
  1504. dev_err(adev->dev, "still active bo inside vm\n");
  1505. }
  1506. rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
  1507. list_del(&mapping->list);
  1508. interval_tree_remove(&mapping->it, &vm->va);
  1509. kfree(mapping);
  1510. }
  1511. list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
  1512. if (mapping->flags & AMDGPU_PTE_PRT && !prt_fini_called) {
  1513. amdgpu_vm_prt_fini(adev, vm);
  1514. prt_fini_called = true;
  1515. }
  1516. list_del(&mapping->list);
  1517. amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
  1518. }
  1519. for (i = 0; i < amdgpu_vm_num_pdes(adev); i++) {
  1520. struct amdgpu_bo *pt = vm->page_tables[i].bo;
  1521. if (!pt)
  1522. continue;
  1523. amdgpu_bo_unref(&pt->shadow);
  1524. amdgpu_bo_unref(&pt);
  1525. }
  1526. drm_free_large(vm->page_tables);
  1527. amdgpu_bo_unref(&vm->page_directory->shadow);
  1528. amdgpu_bo_unref(&vm->page_directory);
  1529. dma_fence_put(vm->page_directory_fence);
  1530. }
  1531. /**
  1532. * amdgpu_vm_manager_init - init the VM manager
  1533. *
  1534. * @adev: amdgpu_device pointer
  1535. *
  1536. * Initialize the VM manager structures
  1537. */
  1538. void amdgpu_vm_manager_init(struct amdgpu_device *adev)
  1539. {
  1540. unsigned i;
  1541. INIT_LIST_HEAD(&adev->vm_manager.ids_lru);
  1542. /* skip over VMID 0, since it is the system VM */
  1543. for (i = 1; i < adev->vm_manager.num_ids; ++i) {
  1544. amdgpu_vm_reset_id(adev, i);
  1545. amdgpu_sync_create(&adev->vm_manager.ids[i].active);
  1546. list_add_tail(&adev->vm_manager.ids[i].list,
  1547. &adev->vm_manager.ids_lru);
  1548. }
  1549. adev->vm_manager.fence_context =
  1550. dma_fence_context_alloc(AMDGPU_MAX_RINGS);
  1551. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  1552. adev->vm_manager.seqno[i] = 0;
  1553. atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
  1554. atomic64_set(&adev->vm_manager.client_counter, 0);
  1555. spin_lock_init(&adev->vm_manager.prt_lock);
  1556. atomic_set(&adev->vm_manager.num_prt_users, 0);
  1557. }
  1558. /**
  1559. * amdgpu_vm_manager_fini - cleanup VM manager
  1560. *
  1561. * @adev: amdgpu_device pointer
  1562. *
  1563. * Cleanup the VM manager and free resources.
  1564. */
  1565. void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
  1566. {
  1567. unsigned i;
  1568. for (i = 0; i < AMDGPU_NUM_VM; ++i) {
  1569. struct amdgpu_vm_id *id = &adev->vm_manager.ids[i];
  1570. dma_fence_put(adev->vm_manager.ids[i].first);
  1571. amdgpu_sync_free(&adev->vm_manager.ids[i].active);
  1572. dma_fence_put(id->flushed_updates);
  1573. dma_fence_put(id->last_flush);
  1574. }
  1575. }