amdgpu_vm.c 32 KB

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