radeon_vm.c 32 KB

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