amdgpu_vm.c 47 KB

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