radeon_vm.c 32 KB

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