amdgpu_vm.c 31 KB

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