amdgpu_vm.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  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 <drm/drmP.h>
  29. #include <drm/amdgpu_drm.h>
  30. #include "amdgpu.h"
  31. #include "amdgpu_trace.h"
  32. /*
  33. * GPUVM
  34. * GPUVM is similar to the legacy gart on older asics, however
  35. * rather than there being a single global gart table
  36. * for the entire GPU, there are multiple VM page tables active
  37. * at any given time. The VM page tables can contain a mix
  38. * vram pages and system memory pages and system memory pages
  39. * can be mapped as snooped (cached system pages) or unsnooped
  40. * (uncached system pages).
  41. * Each VM has an ID associated with it and there is a page table
  42. * associated with each VMID. When execting a command buffer,
  43. * the kernel tells the the ring what VMID to use for that command
  44. * buffer. VMIDs are allocated dynamically as commands are submitted.
  45. * The userspace drivers maintain their own address space and the kernel
  46. * sets up their pages tables accordingly when they submit their
  47. * command buffers and a VMID is assigned.
  48. * Cayman/Trinity support up to 8 active VMs at any given time;
  49. * SI supports 16.
  50. */
  51. /**
  52. * amdgpu_vm_num_pde - return the number of page directory entries
  53. *
  54. * @adev: amdgpu_device pointer
  55. *
  56. * Calculate the number of page directory entries (cayman+).
  57. */
  58. static unsigned amdgpu_vm_num_pdes(struct amdgpu_device *adev)
  59. {
  60. return adev->vm_manager.max_pfn >> amdgpu_vm_block_size;
  61. }
  62. /**
  63. * amdgpu_vm_directory_size - returns the size of the page directory in bytes
  64. *
  65. * @adev: amdgpu_device pointer
  66. *
  67. * Calculate the size of the page directory in bytes (cayman+).
  68. */
  69. static unsigned amdgpu_vm_directory_size(struct amdgpu_device *adev)
  70. {
  71. return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_pdes(adev) * 8);
  72. }
  73. /**
  74. * amdgpu_vm_get_bos - add the vm BOs to a validation list
  75. *
  76. * @vm: vm providing the BOs
  77. * @head: head of validation list
  78. *
  79. * Add the page directory to the list of BOs to
  80. * validate for command submission (cayman+).
  81. */
  82. struct amdgpu_bo_list_entry *amdgpu_vm_get_bos(struct amdgpu_device *adev,
  83. struct amdgpu_vm *vm,
  84. struct list_head *head)
  85. {
  86. struct amdgpu_bo_list_entry *list;
  87. unsigned i, idx;
  88. mutex_lock(&vm->mutex);
  89. list = drm_malloc_ab(vm->max_pde_used + 2,
  90. sizeof(struct amdgpu_bo_list_entry));
  91. if (!list) {
  92. mutex_unlock(&vm->mutex);
  93. return NULL;
  94. }
  95. /* add the vm page table to the list */
  96. list[0].robj = vm->page_directory;
  97. list[0].prefered_domains = AMDGPU_GEM_DOMAIN_VRAM;
  98. list[0].allowed_domains = AMDGPU_GEM_DOMAIN_VRAM;
  99. list[0].priority = 0;
  100. list[0].tv.bo = &vm->page_directory->tbo;
  101. list[0].tv.shared = true;
  102. list_add(&list[0].tv.head, head);
  103. for (i = 0, idx = 1; i <= vm->max_pde_used; i++) {
  104. if (!vm->page_tables[i].bo)
  105. continue;
  106. list[idx].robj = vm->page_tables[i].bo;
  107. list[idx].prefered_domains = AMDGPU_GEM_DOMAIN_VRAM;
  108. list[idx].allowed_domains = AMDGPU_GEM_DOMAIN_VRAM;
  109. list[idx].priority = 0;
  110. list[idx].tv.bo = &list[idx].robj->tbo;
  111. list[idx].tv.shared = true;
  112. list_add(&list[idx++].tv.head, head);
  113. }
  114. mutex_unlock(&vm->mutex);
  115. return list;
  116. }
  117. /**
  118. * amdgpu_vm_grab_id - allocate the next free VMID
  119. *
  120. * @ring: ring we want to submit job to
  121. * @vm: vm to allocate id for
  122. *
  123. * Allocate an id for the vm (cayman+).
  124. * Returns the fence we need to sync to (if any).
  125. *
  126. * Global and local mutex must be locked!
  127. */
  128. struct amdgpu_fence *amdgpu_vm_grab_id(struct amdgpu_ring *ring,
  129. struct amdgpu_vm *vm)
  130. {
  131. struct amdgpu_fence *best[AMDGPU_MAX_RINGS] = {};
  132. struct amdgpu_vm_id *vm_id = &vm->ids[ring->idx];
  133. struct amdgpu_device *adev = ring->adev;
  134. unsigned choices[2] = {};
  135. unsigned i;
  136. /* check if the id is still valid */
  137. if (vm_id->id && vm_id->last_id_use &&
  138. vm_id->last_id_use == adev->vm_manager.active[vm_id->id])
  139. return NULL;
  140. /* we definately need to flush */
  141. vm_id->pd_gpu_addr = ~0ll;
  142. /* skip over VMID 0, since it is the system VM */
  143. for (i = 1; i < adev->vm_manager.nvm; ++i) {
  144. struct amdgpu_fence *fence = adev->vm_manager.active[i];
  145. if (fence == NULL) {
  146. /* found a free one */
  147. vm_id->id = i;
  148. trace_amdgpu_vm_grab_id(i, ring->idx);
  149. return NULL;
  150. }
  151. if (amdgpu_fence_is_earlier(fence, best[fence->ring->idx])) {
  152. best[fence->ring->idx] = fence;
  153. choices[fence->ring == ring ? 0 : 1] = i;
  154. }
  155. }
  156. for (i = 0; i < 2; ++i) {
  157. if (choices[i]) {
  158. vm_id->id = choices[i];
  159. trace_amdgpu_vm_grab_id(choices[i], ring->idx);
  160. return adev->vm_manager.active[choices[i]];
  161. }
  162. }
  163. /* should never happen */
  164. BUG();
  165. return NULL;
  166. }
  167. /**
  168. * amdgpu_vm_flush - hardware flush the vm
  169. *
  170. * @ring: ring to use for flush
  171. * @vm: vm we want to flush
  172. * @updates: last vm update that we waited for
  173. *
  174. * Flush the vm (cayman+).
  175. *
  176. * Global and local mutex must be locked!
  177. */
  178. void amdgpu_vm_flush(struct amdgpu_ring *ring,
  179. struct amdgpu_vm *vm,
  180. struct amdgpu_fence *updates)
  181. {
  182. uint64_t pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
  183. struct amdgpu_vm_id *vm_id = &vm->ids[ring->idx];
  184. if (pd_addr != vm_id->pd_gpu_addr || !vm_id->flushed_updates ||
  185. amdgpu_fence_is_earlier(vm_id->flushed_updates, updates)) {
  186. trace_amdgpu_vm_flush(pd_addr, ring->idx, vm_id->id);
  187. amdgpu_fence_unref(&vm_id->flushed_updates);
  188. vm_id->flushed_updates = amdgpu_fence_ref(updates);
  189. vm_id->pd_gpu_addr = pd_addr;
  190. amdgpu_ring_emit_vm_flush(ring, vm_id->id, vm_id->pd_gpu_addr);
  191. }
  192. }
  193. /**
  194. * amdgpu_vm_fence - remember fence for vm
  195. *
  196. * @adev: amdgpu_device pointer
  197. * @vm: vm we want to fence
  198. * @fence: fence to remember
  199. *
  200. * Fence the vm (cayman+).
  201. * Set the fence used to protect page table and id.
  202. *
  203. * Global and local mutex must be locked!
  204. */
  205. void amdgpu_vm_fence(struct amdgpu_device *adev,
  206. struct amdgpu_vm *vm,
  207. struct amdgpu_fence *fence)
  208. {
  209. unsigned ridx = fence->ring->idx;
  210. unsigned vm_id = vm->ids[ridx].id;
  211. amdgpu_fence_unref(&adev->vm_manager.active[vm_id]);
  212. adev->vm_manager.active[vm_id] = amdgpu_fence_ref(fence);
  213. amdgpu_fence_unref(&vm->ids[ridx].last_id_use);
  214. vm->ids[ridx].last_id_use = amdgpu_fence_ref(fence);
  215. }
  216. /**
  217. * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
  218. *
  219. * @vm: requested vm
  220. * @bo: requested buffer object
  221. *
  222. * Find @bo inside the requested vm (cayman+).
  223. * Search inside the @bos vm list for the requested vm
  224. * Returns the found bo_va or NULL if none is found
  225. *
  226. * Object has to be reserved!
  227. */
  228. struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
  229. struct amdgpu_bo *bo)
  230. {
  231. struct amdgpu_bo_va *bo_va;
  232. list_for_each_entry(bo_va, &bo->va, bo_list) {
  233. if (bo_va->vm == vm) {
  234. return bo_va;
  235. }
  236. }
  237. return NULL;
  238. }
  239. /**
  240. * amdgpu_vm_update_pages - helper to call the right asic function
  241. *
  242. * @adev: amdgpu_device pointer
  243. * @ib: indirect buffer to fill with commands
  244. * @pe: addr of the page entry
  245. * @addr: dst addr to write into pe
  246. * @count: number of page entries to update
  247. * @incr: increase next addr by incr bytes
  248. * @flags: hw access flags
  249. * @gtt_flags: GTT hw access flags
  250. *
  251. * Traces the parameters and calls the right asic functions
  252. * to setup the page table using the DMA.
  253. */
  254. static void amdgpu_vm_update_pages(struct amdgpu_device *adev,
  255. struct amdgpu_ib *ib,
  256. uint64_t pe, uint64_t addr,
  257. unsigned count, uint32_t incr,
  258. uint32_t flags, uint32_t gtt_flags)
  259. {
  260. trace_amdgpu_vm_set_page(pe, addr, count, incr, flags);
  261. if ((flags & AMDGPU_PTE_SYSTEM) && (flags == gtt_flags)) {
  262. uint64_t src = adev->gart.table_addr + (addr >> 12) * 8;
  263. amdgpu_vm_copy_pte(adev, ib, pe, src, count);
  264. } else if ((flags & AMDGPU_PTE_SYSTEM) || (count < 3)) {
  265. amdgpu_vm_write_pte(adev, ib, pe, addr,
  266. count, incr, flags);
  267. } else {
  268. amdgpu_vm_set_pte_pde(adev, ib, pe, addr,
  269. count, incr, flags);
  270. }
  271. }
  272. /**
  273. * amdgpu_vm_clear_bo - initially clear the page dir/table
  274. *
  275. * @adev: amdgpu_device pointer
  276. * @bo: bo to clear
  277. */
  278. static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
  279. struct amdgpu_bo *bo)
  280. {
  281. struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
  282. struct amdgpu_ib ib;
  283. unsigned entries;
  284. uint64_t addr;
  285. int r;
  286. r = amdgpu_bo_reserve(bo, false);
  287. if (r)
  288. return r;
  289. r = reservation_object_reserve_shared(bo->tbo.resv);
  290. if (r)
  291. return r;
  292. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  293. if (r)
  294. goto error_unreserve;
  295. addr = amdgpu_bo_gpu_offset(bo);
  296. entries = amdgpu_bo_size(bo) / 8;
  297. r = amdgpu_ib_get(ring, NULL, entries * 2 + 64, &ib);
  298. if (r)
  299. goto error_unreserve;
  300. ib.length_dw = 0;
  301. amdgpu_vm_update_pages(adev, &ib, addr, 0, entries, 0, 0, 0);
  302. amdgpu_vm_pad_ib(adev, &ib);
  303. WARN_ON(ib.length_dw > 64);
  304. r = amdgpu_ib_schedule(adev, 1, &ib, AMDGPU_FENCE_OWNER_VM);
  305. if (r)
  306. goto error_free;
  307. amdgpu_bo_fence(bo, ib.fence, true);
  308. error_free:
  309. amdgpu_ib_free(adev, &ib);
  310. error_unreserve:
  311. amdgpu_bo_unreserve(bo);
  312. return r;
  313. }
  314. /**
  315. * amdgpu_vm_map_gart - get the physical address of a gart page
  316. *
  317. * @adev: amdgpu_device pointer
  318. * @addr: the unmapped addr
  319. *
  320. * Look up the physical address of the page that the pte resolves
  321. * to (cayman+).
  322. * Returns the physical address of the page.
  323. */
  324. uint64_t amdgpu_vm_map_gart(struct amdgpu_device *adev, uint64_t addr)
  325. {
  326. uint64_t result;
  327. /* page table offset */
  328. result = adev->gart.pages_addr[addr >> PAGE_SHIFT];
  329. /* in case cpu page size != gpu page size*/
  330. result |= addr & (~PAGE_MASK);
  331. return result;
  332. }
  333. /**
  334. * amdgpu_vm_update_pdes - make sure that page directory is valid
  335. *
  336. * @adev: amdgpu_device pointer
  337. * @vm: requested vm
  338. * @start: start of GPU address range
  339. * @end: end of GPU address range
  340. *
  341. * Allocates new page tables if necessary
  342. * and updates the page directory (cayman+).
  343. * Returns 0 for success, error for failure.
  344. *
  345. * Global and local mutex must be locked!
  346. */
  347. int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
  348. struct amdgpu_vm *vm)
  349. {
  350. struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
  351. struct amdgpu_bo *pd = vm->page_directory;
  352. uint64_t pd_addr = amdgpu_bo_gpu_offset(pd);
  353. uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
  354. uint64_t last_pde = ~0, last_pt = ~0;
  355. unsigned count = 0, pt_idx, ndw;
  356. struct amdgpu_ib ib;
  357. int r;
  358. /* padding, etc. */
  359. ndw = 64;
  360. /* assume the worst case */
  361. ndw += vm->max_pde_used * 6;
  362. /* update too big for an IB */
  363. if (ndw > 0xfffff)
  364. return -ENOMEM;
  365. r = amdgpu_ib_get(ring, NULL, ndw * 4, &ib);
  366. if (r)
  367. return r;
  368. ib.length_dw = 0;
  369. /* walk over the address space and update the page directory */
  370. for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
  371. struct amdgpu_bo *bo = vm->page_tables[pt_idx].bo;
  372. uint64_t pde, pt;
  373. if (bo == NULL)
  374. continue;
  375. pt = amdgpu_bo_gpu_offset(bo);
  376. if (vm->page_tables[pt_idx].addr == pt)
  377. continue;
  378. vm->page_tables[pt_idx].addr = pt;
  379. pde = pd_addr + pt_idx * 8;
  380. if (((last_pde + 8 * count) != pde) ||
  381. ((last_pt + incr * count) != pt)) {
  382. if (count) {
  383. amdgpu_vm_update_pages(adev, &ib, last_pde,
  384. last_pt, count, incr,
  385. AMDGPU_PTE_VALID, 0);
  386. }
  387. count = 1;
  388. last_pde = pde;
  389. last_pt = pt;
  390. } else {
  391. ++count;
  392. }
  393. }
  394. if (count)
  395. amdgpu_vm_update_pages(adev, &ib, last_pde, last_pt, count,
  396. incr, AMDGPU_PTE_VALID, 0);
  397. if (ib.length_dw != 0) {
  398. amdgpu_vm_pad_ib(adev, &ib);
  399. amdgpu_sync_resv(adev, &ib.sync, pd->tbo.resv, AMDGPU_FENCE_OWNER_VM);
  400. WARN_ON(ib.length_dw > ndw);
  401. r = amdgpu_ib_schedule(adev, 1, &ib, AMDGPU_FENCE_OWNER_VM);
  402. if (r) {
  403. amdgpu_ib_free(adev, &ib);
  404. return r;
  405. }
  406. amdgpu_bo_fence(pd, ib.fence, true);
  407. }
  408. amdgpu_ib_free(adev, &ib);
  409. return 0;
  410. }
  411. /**
  412. * amdgpu_vm_frag_ptes - add fragment information to PTEs
  413. *
  414. * @adev: amdgpu_device pointer
  415. * @ib: IB for the update
  416. * @pe_start: first PTE to handle
  417. * @pe_end: last PTE to handle
  418. * @addr: addr those PTEs should point to
  419. * @flags: hw mapping flags
  420. * @gtt_flags: GTT hw mapping flags
  421. *
  422. * Global and local mutex must be locked!
  423. */
  424. static void amdgpu_vm_frag_ptes(struct amdgpu_device *adev,
  425. struct amdgpu_ib *ib,
  426. uint64_t pe_start, uint64_t pe_end,
  427. uint64_t addr, uint32_t flags,
  428. uint32_t gtt_flags)
  429. {
  430. /**
  431. * The MC L1 TLB supports variable sized pages, based on a fragment
  432. * field in the PTE. When this field is set to a non-zero value, page
  433. * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
  434. * flags are considered valid for all PTEs within the fragment range
  435. * and corresponding mappings are assumed to be physically contiguous.
  436. *
  437. * The L1 TLB can store a single PTE for the whole fragment,
  438. * significantly increasing the space available for translation
  439. * caching. This leads to large improvements in throughput when the
  440. * TLB is under pressure.
  441. *
  442. * The L2 TLB distributes small and large fragments into two
  443. * asymmetric partitions. The large fragment cache is significantly
  444. * larger. Thus, we try to use large fragments wherever possible.
  445. * Userspace can support this by aligning virtual base address and
  446. * allocation size to the fragment size.
  447. */
  448. /* SI and newer are optimized for 64KB */
  449. uint64_t frag_flags = AMDGPU_PTE_FRAG_64KB;
  450. uint64_t frag_align = 0x80;
  451. uint64_t frag_start = ALIGN(pe_start, frag_align);
  452. uint64_t frag_end = pe_end & ~(frag_align - 1);
  453. unsigned count;
  454. /* system pages are non continuously */
  455. if ((flags & AMDGPU_PTE_SYSTEM) || !(flags & AMDGPU_PTE_VALID) ||
  456. (frag_start >= frag_end)) {
  457. count = (pe_end - pe_start) / 8;
  458. amdgpu_vm_update_pages(adev, ib, pe_start, addr, count,
  459. AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags);
  460. return;
  461. }
  462. /* handle the 4K area at the beginning */
  463. if (pe_start != frag_start) {
  464. count = (frag_start - pe_start) / 8;
  465. amdgpu_vm_update_pages(adev, ib, pe_start, addr, count,
  466. AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags);
  467. addr += AMDGPU_GPU_PAGE_SIZE * count;
  468. }
  469. /* handle the area in the middle */
  470. count = (frag_end - frag_start) / 8;
  471. amdgpu_vm_update_pages(adev, ib, frag_start, addr, count,
  472. AMDGPU_GPU_PAGE_SIZE, flags | frag_flags,
  473. gtt_flags);
  474. /* handle the 4K area at the end */
  475. if (frag_end != pe_end) {
  476. addr += AMDGPU_GPU_PAGE_SIZE * count;
  477. count = (pe_end - frag_end) / 8;
  478. amdgpu_vm_update_pages(adev, ib, frag_end, addr, count,
  479. AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags);
  480. }
  481. }
  482. /**
  483. * amdgpu_vm_update_ptes - make sure that page tables are valid
  484. *
  485. * @adev: amdgpu_device pointer
  486. * @vm: requested vm
  487. * @start: start of GPU address range
  488. * @end: end of GPU address range
  489. * @dst: destination address to map to
  490. * @flags: mapping flags
  491. *
  492. * Update the page tables in the range @start - @end (cayman+).
  493. *
  494. * Global and local mutex must be locked!
  495. */
  496. static int amdgpu_vm_update_ptes(struct amdgpu_device *adev,
  497. struct amdgpu_vm *vm,
  498. struct amdgpu_ib *ib,
  499. uint64_t start, uint64_t end,
  500. uint64_t dst, uint32_t flags,
  501. uint32_t gtt_flags)
  502. {
  503. uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
  504. uint64_t last_pte = ~0, last_dst = ~0;
  505. unsigned count = 0;
  506. uint64_t addr;
  507. /* walk over the address space and update the page tables */
  508. for (addr = start; addr < end; ) {
  509. uint64_t pt_idx = addr >> amdgpu_vm_block_size;
  510. struct amdgpu_bo *pt = vm->page_tables[pt_idx].bo;
  511. unsigned nptes;
  512. uint64_t pte;
  513. int r;
  514. amdgpu_sync_resv(adev, &ib->sync, pt->tbo.resv,
  515. AMDGPU_FENCE_OWNER_VM);
  516. r = reservation_object_reserve_shared(pt->tbo.resv);
  517. if (r)
  518. return r;
  519. if ((addr & ~mask) == (end & ~mask))
  520. nptes = end - addr;
  521. else
  522. nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
  523. pte = amdgpu_bo_gpu_offset(pt);
  524. pte += (addr & mask) * 8;
  525. if ((last_pte + 8 * count) != pte) {
  526. if (count) {
  527. amdgpu_vm_frag_ptes(adev, ib, last_pte,
  528. last_pte + 8 * count,
  529. last_dst, flags,
  530. gtt_flags);
  531. }
  532. count = nptes;
  533. last_pte = pte;
  534. last_dst = dst;
  535. } else {
  536. count += nptes;
  537. }
  538. addr += nptes;
  539. dst += nptes * AMDGPU_GPU_PAGE_SIZE;
  540. }
  541. if (count) {
  542. amdgpu_vm_frag_ptes(adev, ib, last_pte,
  543. last_pte + 8 * count,
  544. last_dst, flags, gtt_flags);
  545. }
  546. return 0;
  547. }
  548. /**
  549. * amdgpu_vm_fence_pts - fence page tables after an update
  550. *
  551. * @vm: requested vm
  552. * @start: start of GPU address range
  553. * @end: end of GPU address range
  554. * @fence: fence to use
  555. *
  556. * Fence the page tables in the range @start - @end (cayman+).
  557. *
  558. * Global and local mutex must be locked!
  559. */
  560. static void amdgpu_vm_fence_pts(struct amdgpu_vm *vm,
  561. uint64_t start, uint64_t end,
  562. struct amdgpu_fence *fence)
  563. {
  564. unsigned i;
  565. start >>= amdgpu_vm_block_size;
  566. end >>= amdgpu_vm_block_size;
  567. for (i = start; i <= end; ++i)
  568. amdgpu_bo_fence(vm->page_tables[i].bo, fence, true);
  569. }
  570. /**
  571. * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
  572. *
  573. * @adev: amdgpu_device pointer
  574. * @vm: requested vm
  575. * @mapping: mapped range and flags to use for the update
  576. * @addr: addr to set the area to
  577. * @gtt_flags: flags as they are used for GTT
  578. * @fence: optional resulting fence
  579. *
  580. * Fill in the page table entries for @mapping.
  581. * Returns 0 for success, -EINVAL for failure.
  582. *
  583. * Object have to be reserved and mutex must be locked!
  584. */
  585. static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
  586. struct amdgpu_vm *vm,
  587. struct amdgpu_bo_va_mapping *mapping,
  588. uint64_t addr, uint32_t gtt_flags,
  589. struct amdgpu_fence **fence)
  590. {
  591. struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
  592. unsigned nptes, ncmds, ndw;
  593. uint32_t flags = gtt_flags;
  594. struct amdgpu_ib ib;
  595. int r;
  596. /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
  597. * but in case of something, we filter the flags in first place
  598. */
  599. if (!(mapping->flags & AMDGPU_PTE_READABLE))
  600. flags &= ~AMDGPU_PTE_READABLE;
  601. if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
  602. flags &= ~AMDGPU_PTE_WRITEABLE;
  603. trace_amdgpu_vm_bo_update(mapping);
  604. nptes = mapping->it.last - mapping->it.start + 1;
  605. /*
  606. * reserve space for one command every (1 << BLOCK_SIZE)
  607. * entries or 2k dwords (whatever is smaller)
  608. */
  609. ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
  610. /* padding, etc. */
  611. ndw = 64;
  612. if ((flags & AMDGPU_PTE_SYSTEM) && (flags == gtt_flags)) {
  613. /* only copy commands needed */
  614. ndw += ncmds * 7;
  615. } else if (flags & AMDGPU_PTE_SYSTEM) {
  616. /* header for write data commands */
  617. ndw += ncmds * 4;
  618. /* body of write data command */
  619. ndw += nptes * 2;
  620. } else {
  621. /* set page commands needed */
  622. ndw += ncmds * 10;
  623. /* two extra commands for begin/end of fragment */
  624. ndw += 2 * 10;
  625. }
  626. /* update too big for an IB */
  627. if (ndw > 0xfffff)
  628. return -ENOMEM;
  629. r = amdgpu_ib_get(ring, NULL, ndw * 4, &ib);
  630. if (r)
  631. return r;
  632. ib.length_dw = 0;
  633. if (!(flags & AMDGPU_PTE_VALID)) {
  634. unsigned i;
  635. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  636. struct amdgpu_fence *f = vm->ids[i].last_id_use;
  637. amdgpu_sync_fence(&ib.sync, f);
  638. }
  639. }
  640. r = amdgpu_vm_update_ptes(adev, vm, &ib, mapping->it.start,
  641. mapping->it.last + 1, addr + mapping->offset,
  642. flags, gtt_flags);
  643. if (r) {
  644. amdgpu_ib_free(adev, &ib);
  645. return r;
  646. }
  647. amdgpu_vm_pad_ib(adev, &ib);
  648. WARN_ON(ib.length_dw > ndw);
  649. r = amdgpu_ib_schedule(adev, 1, &ib, AMDGPU_FENCE_OWNER_VM);
  650. if (r) {
  651. amdgpu_ib_free(adev, &ib);
  652. return r;
  653. }
  654. amdgpu_vm_fence_pts(vm, mapping->it.start,
  655. mapping->it.last + 1, ib.fence);
  656. if (fence) {
  657. amdgpu_fence_unref(fence);
  658. *fence = amdgpu_fence_ref(ib.fence);
  659. }
  660. amdgpu_ib_free(adev, &ib);
  661. return 0;
  662. }
  663. /**
  664. * amdgpu_vm_bo_update - update all BO mappings in the vm page table
  665. *
  666. * @adev: amdgpu_device pointer
  667. * @bo_va: requested BO and VM object
  668. * @mem: ttm mem
  669. *
  670. * Fill in the page table entries for @bo_va.
  671. * Returns 0 for success, -EINVAL for failure.
  672. *
  673. * Object have to be reserved and mutex must be locked!
  674. */
  675. int amdgpu_vm_bo_update(struct amdgpu_device *adev,
  676. struct amdgpu_bo_va *bo_va,
  677. struct ttm_mem_reg *mem)
  678. {
  679. struct amdgpu_vm *vm = bo_va->vm;
  680. struct amdgpu_bo_va_mapping *mapping;
  681. uint32_t flags;
  682. uint64_t addr;
  683. int r;
  684. if (mem) {
  685. addr = mem->start << PAGE_SHIFT;
  686. if (mem->mem_type != TTM_PL_TT)
  687. addr += adev->vm_manager.vram_base_offset;
  688. } else {
  689. addr = 0;
  690. }
  691. if (addr == bo_va->addr)
  692. return 0;
  693. flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
  694. list_for_each_entry(mapping, &bo_va->mappings, list) {
  695. r = amdgpu_vm_bo_update_mapping(adev, vm, mapping, addr,
  696. flags, &bo_va->last_pt_update);
  697. if (r)
  698. return r;
  699. }
  700. bo_va->addr = addr;
  701. spin_lock(&vm->status_lock);
  702. list_del_init(&bo_va->vm_status);
  703. spin_unlock(&vm->status_lock);
  704. return 0;
  705. }
  706. /**
  707. * amdgpu_vm_clear_freed - clear freed BOs in the PT
  708. *
  709. * @adev: amdgpu_device pointer
  710. * @vm: requested vm
  711. *
  712. * Make sure all freed BOs are cleared in the PT.
  713. * Returns 0 for success.
  714. *
  715. * PTs have to be reserved and mutex must be locked!
  716. */
  717. int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
  718. struct amdgpu_vm *vm)
  719. {
  720. struct amdgpu_bo_va_mapping *mapping;
  721. int r;
  722. while (!list_empty(&vm->freed)) {
  723. mapping = list_first_entry(&vm->freed,
  724. struct amdgpu_bo_va_mapping, list);
  725. list_del(&mapping->list);
  726. r = amdgpu_vm_bo_update_mapping(adev, vm, mapping, 0, 0, NULL);
  727. kfree(mapping);
  728. if (r)
  729. return r;
  730. }
  731. return 0;
  732. }
  733. /**
  734. * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
  735. *
  736. * @adev: amdgpu_device pointer
  737. * @vm: requested vm
  738. *
  739. * Make sure all invalidated BOs are cleared in the PT.
  740. * Returns 0 for success.
  741. *
  742. * PTs have to be reserved and mutex must be locked!
  743. */
  744. int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
  745. struct amdgpu_vm *vm, struct amdgpu_sync *sync)
  746. {
  747. struct amdgpu_bo_va *bo_va = NULL;
  748. int r;
  749. spin_lock(&vm->status_lock);
  750. while (!list_empty(&vm->invalidated)) {
  751. bo_va = list_first_entry(&vm->invalidated,
  752. struct amdgpu_bo_va, vm_status);
  753. spin_unlock(&vm->status_lock);
  754. r = amdgpu_vm_bo_update(adev, bo_va, NULL);
  755. if (r)
  756. return r;
  757. spin_lock(&vm->status_lock);
  758. }
  759. spin_unlock(&vm->status_lock);
  760. if (bo_va)
  761. amdgpu_sync_fence(sync, bo_va->last_pt_update);
  762. return 0;
  763. }
  764. /**
  765. * amdgpu_vm_bo_add - add a bo to a specific vm
  766. *
  767. * @adev: amdgpu_device pointer
  768. * @vm: requested vm
  769. * @bo: amdgpu buffer object
  770. *
  771. * Add @bo into the requested vm (cayman+).
  772. * Add @bo to the list of bos associated with the vm
  773. * Returns newly added bo_va or NULL for failure
  774. *
  775. * Object has to be reserved!
  776. */
  777. struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
  778. struct amdgpu_vm *vm,
  779. struct amdgpu_bo *bo)
  780. {
  781. struct amdgpu_bo_va *bo_va;
  782. bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
  783. if (bo_va == NULL) {
  784. return NULL;
  785. }
  786. bo_va->vm = vm;
  787. bo_va->bo = bo;
  788. bo_va->addr = 0;
  789. bo_va->ref_count = 1;
  790. INIT_LIST_HEAD(&bo_va->bo_list);
  791. INIT_LIST_HEAD(&bo_va->mappings);
  792. INIT_LIST_HEAD(&bo_va->vm_status);
  793. mutex_lock(&vm->mutex);
  794. list_add_tail(&bo_va->bo_list, &bo->va);
  795. mutex_unlock(&vm->mutex);
  796. return bo_va;
  797. }
  798. /**
  799. * amdgpu_vm_bo_map - map bo inside a vm
  800. *
  801. * @adev: amdgpu_device pointer
  802. * @bo_va: bo_va to store the address
  803. * @saddr: where to map the BO
  804. * @offset: requested offset in the BO
  805. * @flags: attributes of pages (read/write/valid/etc.)
  806. *
  807. * Add a mapping of the BO at the specefied addr into the VM.
  808. * Returns 0 for success, error for failure.
  809. *
  810. * Object has to be reserved and gets unreserved by this function!
  811. */
  812. int amdgpu_vm_bo_map(struct amdgpu_device *adev,
  813. struct amdgpu_bo_va *bo_va,
  814. uint64_t saddr, uint64_t offset,
  815. uint64_t size, uint32_t flags)
  816. {
  817. struct amdgpu_bo_va_mapping *mapping;
  818. struct amdgpu_vm *vm = bo_va->vm;
  819. struct interval_tree_node *it;
  820. unsigned last_pfn, pt_idx;
  821. uint64_t eaddr;
  822. int r;
  823. /* validate the parameters */
  824. if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
  825. size == 0 || size & AMDGPU_GPU_PAGE_MASK) {
  826. amdgpu_bo_unreserve(bo_va->bo);
  827. return -EINVAL;
  828. }
  829. /* make sure object fit at this offset */
  830. eaddr = saddr + size;
  831. if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo))) {
  832. amdgpu_bo_unreserve(bo_va->bo);
  833. return -EINVAL;
  834. }
  835. last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
  836. if (last_pfn > adev->vm_manager.max_pfn) {
  837. dev_err(adev->dev, "va above limit (0x%08X > 0x%08X)\n",
  838. last_pfn, adev->vm_manager.max_pfn);
  839. amdgpu_bo_unreserve(bo_va->bo);
  840. return -EINVAL;
  841. }
  842. mutex_lock(&vm->mutex);
  843. saddr /= AMDGPU_GPU_PAGE_SIZE;
  844. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  845. it = interval_tree_iter_first(&vm->va, saddr, eaddr - 1);
  846. if (it) {
  847. struct amdgpu_bo_va_mapping *tmp;
  848. tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
  849. /* bo and tmp overlap, invalid addr */
  850. dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
  851. "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
  852. tmp->it.start, tmp->it.last + 1);
  853. amdgpu_bo_unreserve(bo_va->bo);
  854. r = -EINVAL;
  855. goto error_unlock;
  856. }
  857. mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
  858. if (!mapping) {
  859. amdgpu_bo_unreserve(bo_va->bo);
  860. r = -ENOMEM;
  861. goto error_unlock;
  862. }
  863. INIT_LIST_HEAD(&mapping->list);
  864. mapping->it.start = saddr;
  865. mapping->it.last = eaddr - 1;
  866. mapping->offset = offset;
  867. mapping->flags = flags;
  868. list_add(&mapping->list, &bo_va->mappings);
  869. interval_tree_insert(&mapping->it, &vm->va);
  870. trace_amdgpu_vm_bo_map(bo_va, mapping);
  871. bo_va->addr = 0;
  872. /* Make sure the page tables are allocated */
  873. saddr >>= amdgpu_vm_block_size;
  874. eaddr >>= amdgpu_vm_block_size;
  875. BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
  876. if (eaddr > vm->max_pde_used)
  877. vm->max_pde_used = eaddr;
  878. amdgpu_bo_unreserve(bo_va->bo);
  879. /* walk over the address space and allocate the page tables */
  880. for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
  881. struct amdgpu_bo *pt;
  882. if (vm->page_tables[pt_idx].bo)
  883. continue;
  884. /* drop mutex to allocate and clear page table */
  885. mutex_unlock(&vm->mutex);
  886. r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
  887. AMDGPU_GPU_PAGE_SIZE, true,
  888. AMDGPU_GEM_DOMAIN_VRAM, 0, NULL, &pt);
  889. if (r)
  890. goto error_free;
  891. r = amdgpu_vm_clear_bo(adev, pt);
  892. if (r) {
  893. amdgpu_bo_unref(&pt);
  894. goto error_free;
  895. }
  896. /* aquire mutex again */
  897. mutex_lock(&vm->mutex);
  898. if (vm->page_tables[pt_idx].bo) {
  899. /* someone else allocated the pt in the meantime */
  900. mutex_unlock(&vm->mutex);
  901. amdgpu_bo_unref(&pt);
  902. mutex_lock(&vm->mutex);
  903. continue;
  904. }
  905. vm->page_tables[pt_idx].addr = 0;
  906. vm->page_tables[pt_idx].bo = pt;
  907. }
  908. mutex_unlock(&vm->mutex);
  909. return 0;
  910. error_free:
  911. mutex_lock(&vm->mutex);
  912. list_del(&mapping->list);
  913. interval_tree_remove(&mapping->it, &vm->va);
  914. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  915. kfree(mapping);
  916. error_unlock:
  917. mutex_unlock(&vm->mutex);
  918. return r;
  919. }
  920. /**
  921. * amdgpu_vm_bo_unmap - remove bo mapping from vm
  922. *
  923. * @adev: amdgpu_device pointer
  924. * @bo_va: bo_va to remove the address from
  925. * @saddr: where to the BO is mapped
  926. *
  927. * Remove a mapping of the BO at the specefied addr from the VM.
  928. * Returns 0 for success, error for failure.
  929. *
  930. * Object has to be reserved and gets unreserved by this function!
  931. */
  932. int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
  933. struct amdgpu_bo_va *bo_va,
  934. uint64_t saddr)
  935. {
  936. struct amdgpu_bo_va_mapping *mapping;
  937. struct amdgpu_vm *vm = bo_va->vm;
  938. saddr /= AMDGPU_GPU_PAGE_SIZE;
  939. list_for_each_entry(mapping, &bo_va->mappings, list) {
  940. if (mapping->it.start == saddr)
  941. break;
  942. }
  943. if (&mapping->list == &bo_va->mappings) {
  944. amdgpu_bo_unreserve(bo_va->bo);
  945. return -ENOENT;
  946. }
  947. mutex_lock(&vm->mutex);
  948. list_del(&mapping->list);
  949. interval_tree_remove(&mapping->it, &vm->va);
  950. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  951. if (bo_va->addr) {
  952. /* clear the old address */
  953. list_add(&mapping->list, &vm->freed);
  954. } else {
  955. kfree(mapping);
  956. }
  957. mutex_unlock(&vm->mutex);
  958. amdgpu_bo_unreserve(bo_va->bo);
  959. return 0;
  960. }
  961. /**
  962. * amdgpu_vm_bo_rmv - remove a bo to a specific vm
  963. *
  964. * @adev: amdgpu_device pointer
  965. * @bo_va: requested bo_va
  966. *
  967. * Remove @bo_va->bo from the requested vm (cayman+).
  968. *
  969. * Object have to be reserved!
  970. */
  971. void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
  972. struct amdgpu_bo_va *bo_va)
  973. {
  974. struct amdgpu_bo_va_mapping *mapping, *next;
  975. struct amdgpu_vm *vm = bo_va->vm;
  976. list_del(&bo_va->bo_list);
  977. mutex_lock(&vm->mutex);
  978. spin_lock(&vm->status_lock);
  979. list_del(&bo_va->vm_status);
  980. spin_unlock(&vm->status_lock);
  981. list_for_each_entry_safe(mapping, next, &bo_va->mappings, list) {
  982. list_del(&mapping->list);
  983. interval_tree_remove(&mapping->it, &vm->va);
  984. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  985. if (bo_va->addr)
  986. list_add(&mapping->list, &vm->freed);
  987. else
  988. kfree(mapping);
  989. }
  990. amdgpu_fence_unref(&bo_va->last_pt_update);
  991. kfree(bo_va);
  992. mutex_unlock(&vm->mutex);
  993. }
  994. /**
  995. * amdgpu_vm_bo_invalidate - mark the bo as invalid
  996. *
  997. * @adev: amdgpu_device pointer
  998. * @vm: requested vm
  999. * @bo: amdgpu buffer object
  1000. *
  1001. * Mark @bo as invalid (cayman+).
  1002. */
  1003. void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
  1004. struct amdgpu_bo *bo)
  1005. {
  1006. struct amdgpu_bo_va *bo_va;
  1007. list_for_each_entry(bo_va, &bo->va, bo_list) {
  1008. if (bo_va->addr) {
  1009. spin_lock(&bo_va->vm->status_lock);
  1010. list_del(&bo_va->vm_status);
  1011. list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
  1012. spin_unlock(&bo_va->vm->status_lock);
  1013. }
  1014. }
  1015. }
  1016. /**
  1017. * amdgpu_vm_init - initialize a vm instance
  1018. *
  1019. * @adev: amdgpu_device pointer
  1020. * @vm: requested vm
  1021. *
  1022. * Init @vm fields (cayman+).
  1023. */
  1024. int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  1025. {
  1026. const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
  1027. AMDGPU_VM_PTE_COUNT * 8);
  1028. unsigned pd_size, pd_entries, pts_size;
  1029. int i, r;
  1030. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  1031. vm->ids[i].id = 0;
  1032. vm->ids[i].flushed_updates = NULL;
  1033. vm->ids[i].last_id_use = NULL;
  1034. }
  1035. mutex_init(&vm->mutex);
  1036. vm->va = RB_ROOT;
  1037. spin_lock_init(&vm->status_lock);
  1038. INIT_LIST_HEAD(&vm->invalidated);
  1039. INIT_LIST_HEAD(&vm->freed);
  1040. pd_size = amdgpu_vm_directory_size(adev);
  1041. pd_entries = amdgpu_vm_num_pdes(adev);
  1042. /* allocate page table array */
  1043. pts_size = pd_entries * sizeof(struct amdgpu_vm_pt);
  1044. vm->page_tables = kzalloc(pts_size, GFP_KERNEL);
  1045. if (vm->page_tables == NULL) {
  1046. DRM_ERROR("Cannot allocate memory for page table array\n");
  1047. return -ENOMEM;
  1048. }
  1049. r = amdgpu_bo_create(adev, pd_size, align, true,
  1050. AMDGPU_GEM_DOMAIN_VRAM, 0,
  1051. NULL, &vm->page_directory);
  1052. if (r)
  1053. return r;
  1054. r = amdgpu_vm_clear_bo(adev, vm->page_directory);
  1055. if (r) {
  1056. amdgpu_bo_unref(&vm->page_directory);
  1057. vm->page_directory = NULL;
  1058. return r;
  1059. }
  1060. return 0;
  1061. }
  1062. /**
  1063. * amdgpu_vm_fini - tear down a vm instance
  1064. *
  1065. * @adev: amdgpu_device pointer
  1066. * @vm: requested vm
  1067. *
  1068. * Tear down @vm (cayman+).
  1069. * Unbind the VM and remove all bos from the vm bo list
  1070. */
  1071. void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  1072. {
  1073. struct amdgpu_bo_va_mapping *mapping, *tmp;
  1074. int i;
  1075. if (!RB_EMPTY_ROOT(&vm->va)) {
  1076. dev_err(adev->dev, "still active bo inside vm\n");
  1077. }
  1078. rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
  1079. list_del(&mapping->list);
  1080. interval_tree_remove(&mapping->it, &vm->va);
  1081. kfree(mapping);
  1082. }
  1083. list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
  1084. list_del(&mapping->list);
  1085. kfree(mapping);
  1086. }
  1087. for (i = 0; i < amdgpu_vm_num_pdes(adev); i++)
  1088. amdgpu_bo_unref(&vm->page_tables[i].bo);
  1089. kfree(vm->page_tables);
  1090. amdgpu_bo_unref(&vm->page_directory);
  1091. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  1092. amdgpu_fence_unref(&vm->ids[i].flushed_updates);
  1093. amdgpu_fence_unref(&vm->ids[i].last_id_use);
  1094. }
  1095. mutex_destroy(&vm->mutex);
  1096. }