radeon_gart.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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_reg.h"
  32. /*
  33. * GART
  34. * The GART (Graphics Aperture Remapping Table) is an aperture
  35. * in the GPU's address space. System pages can be mapped into
  36. * the aperture and look like contiguous pages from the GPU's
  37. * perspective. A page table maps the pages in the aperture
  38. * to the actual backing pages in system memory.
  39. *
  40. * Radeon GPUs support both an internal GART, as described above,
  41. * and AGP. AGP works similarly, but the GART table is configured
  42. * and maintained by the northbridge rather than the driver.
  43. * Radeon hw has a separate AGP aperture that is programmed to
  44. * point to the AGP aperture provided by the northbridge and the
  45. * requests are passed through to the northbridge aperture.
  46. * Both AGP and internal GART can be used at the same time, however
  47. * that is not currently supported by the driver.
  48. *
  49. * This file handles the common internal GART management.
  50. */
  51. /*
  52. * Common GART table functions.
  53. */
  54. /**
  55. * radeon_gart_table_ram_alloc - allocate system ram for gart page table
  56. *
  57. * @rdev: radeon_device pointer
  58. *
  59. * Allocate system memory for GART page table
  60. * (r1xx-r3xx, non-pcie r4xx, rs400). These asics require the
  61. * gart table to be in system memory.
  62. * Returns 0 for success, -ENOMEM for failure.
  63. */
  64. int radeon_gart_table_ram_alloc(struct radeon_device *rdev)
  65. {
  66. void *ptr;
  67. ptr = pci_alloc_consistent(rdev->pdev, rdev->gart.table_size,
  68. &rdev->gart.table_addr);
  69. if (ptr == NULL) {
  70. return -ENOMEM;
  71. }
  72. #ifdef CONFIG_X86
  73. if (rdev->family == CHIP_RS400 || rdev->family == CHIP_RS480 ||
  74. rdev->family == CHIP_RS690 || rdev->family == CHIP_RS740) {
  75. set_memory_uc((unsigned long)ptr,
  76. rdev->gart.table_size >> PAGE_SHIFT);
  77. }
  78. #endif
  79. rdev->gart.ptr = ptr;
  80. memset((void *)rdev->gart.ptr, 0, rdev->gart.table_size);
  81. return 0;
  82. }
  83. /**
  84. * radeon_gart_table_ram_free - free system ram for gart page table
  85. *
  86. * @rdev: radeon_device pointer
  87. *
  88. * Free system memory for GART page table
  89. * (r1xx-r3xx, non-pcie r4xx, rs400). These asics require the
  90. * gart table to be in system memory.
  91. */
  92. void radeon_gart_table_ram_free(struct radeon_device *rdev)
  93. {
  94. if (rdev->gart.ptr == NULL) {
  95. return;
  96. }
  97. #ifdef CONFIG_X86
  98. if (rdev->family == CHIP_RS400 || rdev->family == CHIP_RS480 ||
  99. rdev->family == CHIP_RS690 || rdev->family == CHIP_RS740) {
  100. set_memory_wb((unsigned long)rdev->gart.ptr,
  101. rdev->gart.table_size >> PAGE_SHIFT);
  102. }
  103. #endif
  104. pci_free_consistent(rdev->pdev, rdev->gart.table_size,
  105. (void *)rdev->gart.ptr,
  106. rdev->gart.table_addr);
  107. rdev->gart.ptr = NULL;
  108. rdev->gart.table_addr = 0;
  109. }
  110. /**
  111. * radeon_gart_table_vram_alloc - allocate vram for gart page table
  112. *
  113. * @rdev: radeon_device pointer
  114. *
  115. * Allocate video memory for GART page table
  116. * (pcie r4xx, r5xx+). These asics require the
  117. * gart table to be in video memory.
  118. * Returns 0 for success, error for failure.
  119. */
  120. int radeon_gart_table_vram_alloc(struct radeon_device *rdev)
  121. {
  122. int r;
  123. if (rdev->gart.robj == NULL) {
  124. r = radeon_bo_create(rdev, rdev->gart.table_size,
  125. PAGE_SIZE, true, RADEON_GEM_DOMAIN_VRAM,
  126. NULL, &rdev->gart.robj);
  127. if (r) {
  128. return r;
  129. }
  130. }
  131. return 0;
  132. }
  133. /**
  134. * radeon_gart_table_vram_pin - pin gart page table in vram
  135. *
  136. * @rdev: radeon_device pointer
  137. *
  138. * Pin the GART page table in vram so it will not be moved
  139. * by the memory manager (pcie r4xx, r5xx+). These asics require the
  140. * gart table to be in video memory.
  141. * Returns 0 for success, error for failure.
  142. */
  143. int radeon_gart_table_vram_pin(struct radeon_device *rdev)
  144. {
  145. uint64_t gpu_addr;
  146. int r;
  147. r = radeon_bo_reserve(rdev->gart.robj, false);
  148. if (unlikely(r != 0))
  149. return r;
  150. r = radeon_bo_pin(rdev->gart.robj,
  151. RADEON_GEM_DOMAIN_VRAM, &gpu_addr);
  152. if (r) {
  153. radeon_bo_unreserve(rdev->gart.robj);
  154. return r;
  155. }
  156. r = radeon_bo_kmap(rdev->gart.robj, &rdev->gart.ptr);
  157. if (r)
  158. radeon_bo_unpin(rdev->gart.robj);
  159. radeon_bo_unreserve(rdev->gart.robj);
  160. rdev->gart.table_addr = gpu_addr;
  161. return r;
  162. }
  163. /**
  164. * radeon_gart_table_vram_unpin - unpin gart page table in vram
  165. *
  166. * @rdev: radeon_device pointer
  167. *
  168. * Unpin the GART page table in vram (pcie r4xx, r5xx+).
  169. * These asics require the gart table to be in video memory.
  170. */
  171. void radeon_gart_table_vram_unpin(struct radeon_device *rdev)
  172. {
  173. int r;
  174. if (rdev->gart.robj == NULL) {
  175. return;
  176. }
  177. r = radeon_bo_reserve(rdev->gart.robj, false);
  178. if (likely(r == 0)) {
  179. radeon_bo_kunmap(rdev->gart.robj);
  180. radeon_bo_unpin(rdev->gart.robj);
  181. radeon_bo_unreserve(rdev->gart.robj);
  182. rdev->gart.ptr = NULL;
  183. }
  184. }
  185. /**
  186. * radeon_gart_table_vram_free - free gart page table vram
  187. *
  188. * @rdev: radeon_device pointer
  189. *
  190. * Free the video memory used for the GART page table
  191. * (pcie r4xx, r5xx+). These asics require the gart table to
  192. * be in video memory.
  193. */
  194. void radeon_gart_table_vram_free(struct radeon_device *rdev)
  195. {
  196. if (rdev->gart.robj == NULL) {
  197. return;
  198. }
  199. radeon_bo_unref(&rdev->gart.robj);
  200. }
  201. /*
  202. * Common gart functions.
  203. */
  204. /**
  205. * radeon_gart_unbind - unbind pages from the gart page table
  206. *
  207. * @rdev: radeon_device pointer
  208. * @offset: offset into the GPU's gart aperture
  209. * @pages: number of pages to unbind
  210. *
  211. * Unbinds the requested pages from the gart page table and
  212. * replaces them with the dummy page (all asics).
  213. */
  214. void radeon_gart_unbind(struct radeon_device *rdev, unsigned offset,
  215. int pages)
  216. {
  217. unsigned t;
  218. unsigned p;
  219. int i, j;
  220. u64 page_base;
  221. if (!rdev->gart.ready) {
  222. WARN(1, "trying to unbind memory from uninitialized GART !\n");
  223. return;
  224. }
  225. t = offset / RADEON_GPU_PAGE_SIZE;
  226. p = t / (PAGE_SIZE / RADEON_GPU_PAGE_SIZE);
  227. for (i = 0; i < pages; i++, p++) {
  228. if (rdev->gart.pages[p]) {
  229. rdev->gart.pages[p] = NULL;
  230. rdev->gart.pages_addr[p] = rdev->dummy_page.addr;
  231. page_base = rdev->gart.pages_addr[p];
  232. for (j = 0; j < (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); j++, t++) {
  233. if (rdev->gart.ptr) {
  234. radeon_gart_set_page(rdev, t, page_base);
  235. }
  236. page_base += RADEON_GPU_PAGE_SIZE;
  237. }
  238. }
  239. }
  240. mb();
  241. radeon_gart_tlb_flush(rdev);
  242. }
  243. /**
  244. * radeon_gart_bind - bind pages into the gart page table
  245. *
  246. * @rdev: radeon_device pointer
  247. * @offset: offset into the GPU's gart aperture
  248. * @pages: number of pages to bind
  249. * @pagelist: pages to bind
  250. * @dma_addr: DMA addresses of pages
  251. *
  252. * Binds the requested pages to the gart page table
  253. * (all asics).
  254. * Returns 0 for success, -EINVAL for failure.
  255. */
  256. int radeon_gart_bind(struct radeon_device *rdev, unsigned offset,
  257. int pages, struct page **pagelist, dma_addr_t *dma_addr)
  258. {
  259. unsigned t;
  260. unsigned p;
  261. uint64_t page_base;
  262. int i, j;
  263. if (!rdev->gart.ready) {
  264. WARN(1, "trying to bind memory to uninitialized GART !\n");
  265. return -EINVAL;
  266. }
  267. t = offset / RADEON_GPU_PAGE_SIZE;
  268. p = t / (PAGE_SIZE / RADEON_GPU_PAGE_SIZE);
  269. for (i = 0; i < pages; i++, p++) {
  270. rdev->gart.pages_addr[p] = dma_addr[i];
  271. rdev->gart.pages[p] = pagelist[i];
  272. if (rdev->gart.ptr) {
  273. page_base = rdev->gart.pages_addr[p];
  274. for (j = 0; j < (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); j++, t++) {
  275. radeon_gart_set_page(rdev, t, page_base);
  276. page_base += RADEON_GPU_PAGE_SIZE;
  277. }
  278. }
  279. }
  280. mb();
  281. radeon_gart_tlb_flush(rdev);
  282. return 0;
  283. }
  284. /**
  285. * radeon_gart_restore - bind all pages in the gart page table
  286. *
  287. * @rdev: radeon_device pointer
  288. *
  289. * Binds all pages in the gart page table (all asics).
  290. * Used to rebuild the gart table on device startup or resume.
  291. */
  292. void radeon_gart_restore(struct radeon_device *rdev)
  293. {
  294. int i, j, t;
  295. u64 page_base;
  296. if (!rdev->gart.ptr) {
  297. return;
  298. }
  299. for (i = 0, t = 0; i < rdev->gart.num_cpu_pages; i++) {
  300. page_base = rdev->gart.pages_addr[i];
  301. for (j = 0; j < (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); j++, t++) {
  302. radeon_gart_set_page(rdev, t, page_base);
  303. page_base += RADEON_GPU_PAGE_SIZE;
  304. }
  305. }
  306. mb();
  307. radeon_gart_tlb_flush(rdev);
  308. }
  309. /**
  310. * radeon_gart_init - init the driver info for managing the gart
  311. *
  312. * @rdev: radeon_device pointer
  313. *
  314. * Allocate the dummy page and init the gart driver info (all asics).
  315. * Returns 0 for success, error for failure.
  316. */
  317. int radeon_gart_init(struct radeon_device *rdev)
  318. {
  319. int r, i;
  320. if (rdev->gart.pages) {
  321. return 0;
  322. }
  323. /* We need PAGE_SIZE >= RADEON_GPU_PAGE_SIZE */
  324. if (PAGE_SIZE < RADEON_GPU_PAGE_SIZE) {
  325. DRM_ERROR("Page size is smaller than GPU page size!\n");
  326. return -EINVAL;
  327. }
  328. r = radeon_dummy_page_init(rdev);
  329. if (r)
  330. return r;
  331. /* Compute table size */
  332. rdev->gart.num_cpu_pages = rdev->mc.gtt_size / PAGE_SIZE;
  333. rdev->gart.num_gpu_pages = rdev->mc.gtt_size / RADEON_GPU_PAGE_SIZE;
  334. DRM_INFO("GART: num cpu pages %u, num gpu pages %u\n",
  335. rdev->gart.num_cpu_pages, rdev->gart.num_gpu_pages);
  336. /* Allocate pages table */
  337. rdev->gart.pages = vzalloc(sizeof(void *) * rdev->gart.num_cpu_pages);
  338. if (rdev->gart.pages == NULL) {
  339. radeon_gart_fini(rdev);
  340. return -ENOMEM;
  341. }
  342. rdev->gart.pages_addr = vzalloc(sizeof(dma_addr_t) *
  343. rdev->gart.num_cpu_pages);
  344. if (rdev->gart.pages_addr == NULL) {
  345. radeon_gart_fini(rdev);
  346. return -ENOMEM;
  347. }
  348. /* set GART entry to point to the dummy page by default */
  349. for (i = 0; i < rdev->gart.num_cpu_pages; i++) {
  350. rdev->gart.pages_addr[i] = rdev->dummy_page.addr;
  351. }
  352. return 0;
  353. }
  354. /**
  355. * radeon_gart_fini - tear down the driver info for managing the gart
  356. *
  357. * @rdev: radeon_device pointer
  358. *
  359. * Tear down the gart driver info and free the dummy page (all asics).
  360. */
  361. void radeon_gart_fini(struct radeon_device *rdev)
  362. {
  363. if (rdev->gart.pages && rdev->gart.pages_addr && rdev->gart.ready) {
  364. /* unbind pages */
  365. radeon_gart_unbind(rdev, 0, rdev->gart.num_cpu_pages);
  366. }
  367. rdev->gart.ready = false;
  368. vfree(rdev->gart.pages);
  369. vfree(rdev->gart.pages_addr);
  370. rdev->gart.pages = NULL;
  371. rdev->gart.pages_addr = NULL;
  372. radeon_dummy_page_fini(rdev);
  373. }
  374. /*
  375. * GPUVM
  376. * GPUVM is similar to the legacy gart on older asics, however
  377. * rather than there being a single global gart table
  378. * for the entire GPU, there are multiple VM page tables active
  379. * at any given time. The VM page tables can contain a mix
  380. * vram pages and system memory pages and system memory pages
  381. * can be mapped as snooped (cached system pages) or unsnooped
  382. * (uncached system pages).
  383. * Each VM has an ID associated with it and there is a page table
  384. * associated with each VMID. When execting a command buffer,
  385. * the kernel tells the the ring what VMID to use for that command
  386. * buffer. VMIDs are allocated dynamically as commands are submitted.
  387. * The userspace drivers maintain their own address space and the kernel
  388. * sets up their pages tables accordingly when they submit their
  389. * command buffers and a VMID is assigned.
  390. * Cayman/Trinity support up to 8 active VMs at any given time;
  391. * SI supports 16.
  392. */
  393. /*
  394. * vm helpers
  395. *
  396. * TODO bind a default page at vm initialization for default address
  397. */
  398. /**
  399. * radeon_vm_num_pde - return the number of page directory entries
  400. *
  401. * @rdev: radeon_device pointer
  402. *
  403. * Calculate the number of page directory entries (cayman+).
  404. */
  405. static unsigned radeon_vm_num_pdes(struct radeon_device *rdev)
  406. {
  407. return rdev->vm_manager.max_pfn >> RADEON_VM_BLOCK_SIZE;
  408. }
  409. /**
  410. * radeon_vm_directory_size - returns the size of the page directory in bytes
  411. *
  412. * @rdev: radeon_device pointer
  413. *
  414. * Calculate the size of the page directory in bytes (cayman+).
  415. */
  416. static unsigned radeon_vm_directory_size(struct radeon_device *rdev)
  417. {
  418. return RADEON_GPU_PAGE_ALIGN(radeon_vm_num_pdes(rdev) * 8);
  419. }
  420. /**
  421. * radeon_vm_manager_init - init the vm manager
  422. *
  423. * @rdev: radeon_device pointer
  424. *
  425. * Init the vm manager (cayman+).
  426. * Returns 0 for success, error for failure.
  427. */
  428. int radeon_vm_manager_init(struct radeon_device *rdev)
  429. {
  430. struct radeon_vm *vm;
  431. struct radeon_bo_va *bo_va;
  432. int r;
  433. unsigned size;
  434. if (!rdev->vm_manager.enabled) {
  435. /* allocate enough for 2 full VM pts */
  436. size = radeon_vm_directory_size(rdev);
  437. size += rdev->vm_manager.max_pfn * 8;
  438. size *= 2;
  439. r = radeon_sa_bo_manager_init(rdev, &rdev->vm_manager.sa_manager,
  440. RADEON_GPU_PAGE_ALIGN(size),
  441. RADEON_VM_PTB_ALIGN_SIZE,
  442. RADEON_GEM_DOMAIN_VRAM);
  443. if (r) {
  444. dev_err(rdev->dev, "failed to allocate vm bo (%dKB)\n",
  445. (rdev->vm_manager.max_pfn * 8) >> 10);
  446. return r;
  447. }
  448. r = radeon_asic_vm_init(rdev);
  449. if (r)
  450. return r;
  451. rdev->vm_manager.enabled = true;
  452. r = radeon_sa_bo_manager_start(rdev, &rdev->vm_manager.sa_manager);
  453. if (r)
  454. return r;
  455. }
  456. /* restore page table */
  457. list_for_each_entry(vm, &rdev->vm_manager.lru_vm, list) {
  458. if (vm->page_directory == NULL)
  459. continue;
  460. list_for_each_entry(bo_va, &vm->va, vm_list) {
  461. bo_va->valid = false;
  462. }
  463. }
  464. return 0;
  465. }
  466. /**
  467. * radeon_vm_free_pt - free the page table for a specific vm
  468. *
  469. * @rdev: radeon_device pointer
  470. * @vm: vm to unbind
  471. *
  472. * Free the page table of a specific vm (cayman+).
  473. *
  474. * Global and local mutex must be lock!
  475. */
  476. static void radeon_vm_free_pt(struct radeon_device *rdev,
  477. struct radeon_vm *vm)
  478. {
  479. struct radeon_bo_va *bo_va;
  480. int i;
  481. if (!vm->page_directory)
  482. return;
  483. list_del_init(&vm->list);
  484. radeon_sa_bo_free(rdev, &vm->page_directory, vm->fence);
  485. list_for_each_entry(bo_va, &vm->va, vm_list) {
  486. bo_va->valid = false;
  487. }
  488. if (vm->page_tables == NULL)
  489. return;
  490. for (i = 0; i < radeon_vm_num_pdes(rdev); i++)
  491. radeon_sa_bo_free(rdev, &vm->page_tables[i], vm->fence);
  492. kfree(vm->page_tables);
  493. }
  494. /**
  495. * radeon_vm_manager_fini - tear down the vm manager
  496. *
  497. * @rdev: radeon_device pointer
  498. *
  499. * Tear down the VM manager (cayman+).
  500. */
  501. void radeon_vm_manager_fini(struct radeon_device *rdev)
  502. {
  503. struct radeon_vm *vm, *tmp;
  504. int i;
  505. if (!rdev->vm_manager.enabled)
  506. return;
  507. mutex_lock(&rdev->vm_manager.lock);
  508. /* free all allocated page tables */
  509. list_for_each_entry_safe(vm, tmp, &rdev->vm_manager.lru_vm, list) {
  510. mutex_lock(&vm->mutex);
  511. radeon_vm_free_pt(rdev, vm);
  512. mutex_unlock(&vm->mutex);
  513. }
  514. for (i = 0; i < RADEON_NUM_VM; ++i) {
  515. radeon_fence_unref(&rdev->vm_manager.active[i]);
  516. }
  517. radeon_asic_vm_fini(rdev);
  518. mutex_unlock(&rdev->vm_manager.lock);
  519. radeon_sa_bo_manager_suspend(rdev, &rdev->vm_manager.sa_manager);
  520. radeon_sa_bo_manager_fini(rdev, &rdev->vm_manager.sa_manager);
  521. rdev->vm_manager.enabled = false;
  522. }
  523. /**
  524. * radeon_vm_evict - evict page table to make room for new one
  525. *
  526. * @rdev: radeon_device pointer
  527. * @vm: VM we want to allocate something for
  528. *
  529. * Evict a VM from the lru, making sure that it isn't @vm. (cayman+).
  530. * Returns 0 for success, -ENOMEM for failure.
  531. *
  532. * Global and local mutex must be locked!
  533. */
  534. static int radeon_vm_evict(struct radeon_device *rdev, struct radeon_vm *vm)
  535. {
  536. struct radeon_vm *vm_evict;
  537. if (list_empty(&rdev->vm_manager.lru_vm))
  538. return -ENOMEM;
  539. vm_evict = list_first_entry(&rdev->vm_manager.lru_vm,
  540. struct radeon_vm, list);
  541. if (vm_evict == vm)
  542. return -ENOMEM;
  543. mutex_lock(&vm_evict->mutex);
  544. radeon_vm_free_pt(rdev, vm_evict);
  545. mutex_unlock(&vm_evict->mutex);
  546. return 0;
  547. }
  548. /**
  549. * radeon_vm_alloc_pt - allocates a page table for a VM
  550. *
  551. * @rdev: radeon_device pointer
  552. * @vm: vm to bind
  553. *
  554. * Allocate a page table for the requested vm (cayman+).
  555. * Returns 0 for success, error for failure.
  556. *
  557. * Global and local mutex must be locked!
  558. */
  559. int radeon_vm_alloc_pt(struct radeon_device *rdev, struct radeon_vm *vm)
  560. {
  561. unsigned pd_size, pts_size;
  562. u64 *pd_addr;
  563. int r;
  564. if (vm == NULL) {
  565. return -EINVAL;
  566. }
  567. if (vm->page_directory != NULL) {
  568. return 0;
  569. }
  570. retry:
  571. pd_size = radeon_vm_directory_size(rdev);
  572. r = radeon_sa_bo_new(rdev, &rdev->vm_manager.sa_manager,
  573. &vm->page_directory, pd_size,
  574. RADEON_VM_PTB_ALIGN_SIZE, false);
  575. if (r == -ENOMEM) {
  576. r = radeon_vm_evict(rdev, vm);
  577. if (r)
  578. return r;
  579. goto retry;
  580. } else if (r) {
  581. return r;
  582. }
  583. vm->pd_gpu_addr = radeon_sa_bo_gpu_addr(vm->page_directory);
  584. /* Initially clear the page directory */
  585. pd_addr = radeon_sa_bo_cpu_addr(vm->page_directory);
  586. memset(pd_addr, 0, pd_size);
  587. pts_size = radeon_vm_num_pdes(rdev) * sizeof(struct radeon_sa_bo *);
  588. vm->page_tables = kzalloc(pts_size, GFP_KERNEL);
  589. if (vm->page_tables == NULL) {
  590. DRM_ERROR("Cannot allocate memory for page table array\n");
  591. radeon_sa_bo_free(rdev, &vm->page_directory, vm->fence);
  592. return -ENOMEM;
  593. }
  594. return 0;
  595. }
  596. /**
  597. * radeon_vm_add_to_lru - add VMs page table to LRU list
  598. *
  599. * @rdev: radeon_device pointer
  600. * @vm: vm to add to LRU
  601. *
  602. * Add the allocated page table to the LRU list (cayman+).
  603. *
  604. * Global mutex must be locked!
  605. */
  606. void radeon_vm_add_to_lru(struct radeon_device *rdev, struct radeon_vm *vm)
  607. {
  608. list_del_init(&vm->list);
  609. list_add_tail(&vm->list, &rdev->vm_manager.lru_vm);
  610. }
  611. /**
  612. * radeon_vm_grab_id - allocate the next free VMID
  613. *
  614. * @rdev: radeon_device pointer
  615. * @vm: vm to allocate id for
  616. * @ring: ring we want to submit job to
  617. *
  618. * Allocate an id for the vm (cayman+).
  619. * Returns the fence we need to sync to (if any).
  620. *
  621. * Global and local mutex must be locked!
  622. */
  623. struct radeon_fence *radeon_vm_grab_id(struct radeon_device *rdev,
  624. struct radeon_vm *vm, int ring)
  625. {
  626. struct radeon_fence *best[RADEON_NUM_RINGS] = {};
  627. unsigned choices[2] = {};
  628. unsigned i;
  629. /* check if the id is still valid */
  630. if (vm->fence && vm->fence == rdev->vm_manager.active[vm->id])
  631. return NULL;
  632. /* we definately need to flush */
  633. radeon_fence_unref(&vm->last_flush);
  634. /* skip over VMID 0, since it is the system VM */
  635. for (i = 1; i < rdev->vm_manager.nvm; ++i) {
  636. struct radeon_fence *fence = rdev->vm_manager.active[i];
  637. if (fence == NULL) {
  638. /* found a free one */
  639. vm->id = i;
  640. return NULL;
  641. }
  642. if (radeon_fence_is_earlier(fence, best[fence->ring])) {
  643. best[fence->ring] = fence;
  644. choices[fence->ring == ring ? 0 : 1] = i;
  645. }
  646. }
  647. for (i = 0; i < 2; ++i) {
  648. if (choices[i]) {
  649. vm->id = choices[i];
  650. return rdev->vm_manager.active[choices[i]];
  651. }
  652. }
  653. /* should never happen */
  654. BUG();
  655. return NULL;
  656. }
  657. /**
  658. * radeon_vm_fence - remember fence for vm
  659. *
  660. * @rdev: radeon_device pointer
  661. * @vm: vm we want to fence
  662. * @fence: fence to remember
  663. *
  664. * Fence the vm (cayman+).
  665. * Set the fence used to protect page table and id.
  666. *
  667. * Global and local mutex must be locked!
  668. */
  669. void radeon_vm_fence(struct radeon_device *rdev,
  670. struct radeon_vm *vm,
  671. struct radeon_fence *fence)
  672. {
  673. radeon_fence_unref(&rdev->vm_manager.active[vm->id]);
  674. rdev->vm_manager.active[vm->id] = radeon_fence_ref(fence);
  675. radeon_fence_unref(&vm->fence);
  676. vm->fence = radeon_fence_ref(fence);
  677. }
  678. /**
  679. * radeon_vm_bo_find - find the bo_va for a specific vm & bo
  680. *
  681. * @vm: requested vm
  682. * @bo: requested buffer object
  683. *
  684. * Find @bo inside the requested vm (cayman+).
  685. * Search inside the @bos vm list for the requested vm
  686. * Returns the found bo_va or NULL if none is found
  687. *
  688. * Object has to be reserved!
  689. */
  690. struct radeon_bo_va *radeon_vm_bo_find(struct radeon_vm *vm,
  691. struct radeon_bo *bo)
  692. {
  693. struct radeon_bo_va *bo_va;
  694. list_for_each_entry(bo_va, &bo->va, bo_list) {
  695. if (bo_va->vm == vm) {
  696. return bo_va;
  697. }
  698. }
  699. return NULL;
  700. }
  701. /**
  702. * radeon_vm_bo_add - add a bo to a specific vm
  703. *
  704. * @rdev: radeon_device pointer
  705. * @vm: requested vm
  706. * @bo: radeon buffer object
  707. *
  708. * Add @bo into the requested vm (cayman+).
  709. * Add @bo to the list of bos associated with the vm
  710. * Returns newly added bo_va or NULL for failure
  711. *
  712. * Object has to be reserved!
  713. */
  714. struct radeon_bo_va *radeon_vm_bo_add(struct radeon_device *rdev,
  715. struct radeon_vm *vm,
  716. struct radeon_bo *bo)
  717. {
  718. struct radeon_bo_va *bo_va;
  719. bo_va = kzalloc(sizeof(struct radeon_bo_va), GFP_KERNEL);
  720. if (bo_va == NULL) {
  721. return NULL;
  722. }
  723. bo_va->vm = vm;
  724. bo_va->bo = bo;
  725. bo_va->soffset = 0;
  726. bo_va->eoffset = 0;
  727. bo_va->flags = 0;
  728. bo_va->valid = false;
  729. bo_va->ref_count = 1;
  730. INIT_LIST_HEAD(&bo_va->bo_list);
  731. INIT_LIST_HEAD(&bo_va->vm_list);
  732. mutex_lock(&vm->mutex);
  733. list_add(&bo_va->vm_list, &vm->va);
  734. list_add_tail(&bo_va->bo_list, &bo->va);
  735. mutex_unlock(&vm->mutex);
  736. return bo_va;
  737. }
  738. /**
  739. * radeon_vm_bo_set_addr - set bos virtual address inside a vm
  740. *
  741. * @rdev: radeon_device pointer
  742. * @bo_va: bo_va to store the address
  743. * @soffset: requested offset of the buffer in the VM address space
  744. * @flags: attributes of pages (read/write/valid/etc.)
  745. *
  746. * Set offset of @bo_va (cayman+).
  747. * Validate and set the offset requested within the vm address space.
  748. * Returns 0 for success, error for failure.
  749. *
  750. * Object has to be reserved!
  751. */
  752. int radeon_vm_bo_set_addr(struct radeon_device *rdev,
  753. struct radeon_bo_va *bo_va,
  754. uint64_t soffset,
  755. uint32_t flags)
  756. {
  757. uint64_t size = radeon_bo_size(bo_va->bo);
  758. uint64_t eoffset, last_offset = 0;
  759. struct radeon_vm *vm = bo_va->vm;
  760. struct radeon_bo_va *tmp;
  761. struct list_head *head;
  762. unsigned last_pfn;
  763. if (soffset) {
  764. /* make sure object fit at this offset */
  765. eoffset = soffset + size;
  766. if (soffset >= eoffset) {
  767. return -EINVAL;
  768. }
  769. last_pfn = eoffset / RADEON_GPU_PAGE_SIZE;
  770. if (last_pfn > rdev->vm_manager.max_pfn) {
  771. dev_err(rdev->dev, "va above limit (0x%08X > 0x%08X)\n",
  772. last_pfn, rdev->vm_manager.max_pfn);
  773. return -EINVAL;
  774. }
  775. } else {
  776. eoffset = last_pfn = 0;
  777. }
  778. mutex_lock(&vm->mutex);
  779. head = &vm->va;
  780. last_offset = 0;
  781. list_for_each_entry(tmp, &vm->va, vm_list) {
  782. if (bo_va == tmp) {
  783. /* skip over currently modified bo */
  784. continue;
  785. }
  786. if (soffset >= last_offset && eoffset <= tmp->soffset) {
  787. /* bo can be added before this one */
  788. break;
  789. }
  790. if (eoffset > tmp->soffset && soffset < tmp->eoffset) {
  791. /* bo and tmp overlap, invalid offset */
  792. dev_err(rdev->dev, "bo %p va 0x%08X conflict with (bo %p 0x%08X 0x%08X)\n",
  793. bo_va->bo, (unsigned)bo_va->soffset, tmp->bo,
  794. (unsigned)tmp->soffset, (unsigned)tmp->eoffset);
  795. mutex_unlock(&vm->mutex);
  796. return -EINVAL;
  797. }
  798. last_offset = tmp->eoffset;
  799. head = &tmp->vm_list;
  800. }
  801. bo_va->soffset = soffset;
  802. bo_va->eoffset = eoffset;
  803. bo_va->flags = flags;
  804. bo_va->valid = false;
  805. list_move(&bo_va->vm_list, head);
  806. mutex_unlock(&vm->mutex);
  807. return 0;
  808. }
  809. /**
  810. * radeon_vm_map_gart - get the physical address of a gart page
  811. *
  812. * @rdev: radeon_device pointer
  813. * @addr: the unmapped addr
  814. *
  815. * Look up the physical address of the page that the pte resolves
  816. * to (cayman+).
  817. * Returns the physical address of the page.
  818. */
  819. uint64_t radeon_vm_map_gart(struct radeon_device *rdev, uint64_t addr)
  820. {
  821. uint64_t result;
  822. /* page table offset */
  823. result = rdev->gart.pages_addr[addr >> PAGE_SHIFT];
  824. /* in case cpu page size != gpu page size*/
  825. result |= addr & (~PAGE_MASK);
  826. return result;
  827. }
  828. /**
  829. * radeon_vm_update_pdes - make sure that page directory is valid
  830. *
  831. * @rdev: radeon_device pointer
  832. * @vm: requested vm
  833. * @start: start of GPU address range
  834. * @end: end of GPU address range
  835. *
  836. * Allocates new page tables if necessary
  837. * and updates the page directory (cayman+).
  838. * Returns 0 for success, error for failure.
  839. *
  840. * Global and local mutex must be locked!
  841. */
  842. static int radeon_vm_update_pdes(struct radeon_device *rdev,
  843. struct radeon_vm *vm,
  844. struct radeon_ib *ib,
  845. uint64_t start, uint64_t end)
  846. {
  847. static const uint32_t incr = RADEON_VM_PTE_COUNT * 8;
  848. uint64_t last_pde = ~0, last_pt = ~0;
  849. unsigned count = 0;
  850. uint64_t pt_idx;
  851. int r;
  852. start = (start / RADEON_GPU_PAGE_SIZE) >> RADEON_VM_BLOCK_SIZE;
  853. end = (end / RADEON_GPU_PAGE_SIZE) >> RADEON_VM_BLOCK_SIZE;
  854. /* walk over the address space and update the page directory */
  855. for (pt_idx = start; pt_idx <= end; ++pt_idx) {
  856. uint64_t pde, pt;
  857. if (vm->page_tables[pt_idx])
  858. continue;
  859. retry:
  860. r = radeon_sa_bo_new(rdev, &rdev->vm_manager.sa_manager,
  861. &vm->page_tables[pt_idx],
  862. RADEON_VM_PTE_COUNT * 8,
  863. RADEON_GPU_PAGE_SIZE, false);
  864. if (r == -ENOMEM) {
  865. r = radeon_vm_evict(rdev, vm);
  866. if (r)
  867. return r;
  868. goto retry;
  869. } else if (r) {
  870. return r;
  871. }
  872. pde = vm->pd_gpu_addr + pt_idx * 8;
  873. pt = radeon_sa_bo_gpu_addr(vm->page_tables[pt_idx]);
  874. if (((last_pde + 8 * count) != pde) ||
  875. ((last_pt + incr * count) != pt)) {
  876. if (count) {
  877. radeon_asic_vm_set_page(rdev, ib, last_pde,
  878. last_pt, count, incr,
  879. RADEON_VM_PAGE_VALID);
  880. }
  881. count = 1;
  882. last_pde = pde;
  883. last_pt = pt;
  884. } else {
  885. ++count;
  886. }
  887. }
  888. if (count) {
  889. radeon_asic_vm_set_page(rdev, ib, last_pde, last_pt, count,
  890. incr, RADEON_VM_PAGE_VALID);
  891. }
  892. return 0;
  893. }
  894. /**
  895. * radeon_vm_update_ptes - make sure that page tables are valid
  896. *
  897. * @rdev: radeon_device pointer
  898. * @vm: requested vm
  899. * @start: start of GPU address range
  900. * @end: end of GPU address range
  901. * @dst: destination address to map to
  902. * @flags: mapping flags
  903. *
  904. * Update the page tables in the range @start - @end (cayman+).
  905. *
  906. * Global and local mutex must be locked!
  907. */
  908. static void radeon_vm_update_ptes(struct radeon_device *rdev,
  909. struct radeon_vm *vm,
  910. struct radeon_ib *ib,
  911. uint64_t start, uint64_t end,
  912. uint64_t dst, uint32_t flags)
  913. {
  914. static const uint64_t mask = RADEON_VM_PTE_COUNT - 1;
  915. uint64_t last_pte = ~0, last_dst = ~0;
  916. unsigned count = 0;
  917. uint64_t addr;
  918. start = start / RADEON_GPU_PAGE_SIZE;
  919. end = end / RADEON_GPU_PAGE_SIZE;
  920. /* walk over the address space and update the page tables */
  921. for (addr = start; addr < end; ) {
  922. uint64_t pt_idx = addr >> RADEON_VM_BLOCK_SIZE;
  923. unsigned nptes;
  924. uint64_t pte;
  925. if ((addr & ~mask) == (end & ~mask))
  926. nptes = end - addr;
  927. else
  928. nptes = RADEON_VM_PTE_COUNT - (addr & mask);
  929. pte = radeon_sa_bo_gpu_addr(vm->page_tables[pt_idx]);
  930. pte += (addr & mask) * 8;
  931. if ((last_pte + 8 * count) != pte) {
  932. if (count) {
  933. radeon_asic_vm_set_page(rdev, ib, last_pte,
  934. last_dst, count,
  935. RADEON_GPU_PAGE_SIZE,
  936. flags);
  937. }
  938. count = nptes;
  939. last_pte = pte;
  940. last_dst = dst;
  941. } else {
  942. count += nptes;
  943. }
  944. addr += nptes;
  945. dst += nptes * RADEON_GPU_PAGE_SIZE;
  946. }
  947. if (count) {
  948. radeon_asic_vm_set_page(rdev, ib, last_pte,
  949. last_dst, count,
  950. RADEON_GPU_PAGE_SIZE, flags);
  951. }
  952. }
  953. /**
  954. * radeon_vm_bo_update_pte - map a bo into the vm page table
  955. *
  956. * @rdev: radeon_device pointer
  957. * @vm: requested vm
  958. * @bo: radeon buffer object
  959. * @mem: ttm mem
  960. *
  961. * Fill in the page table entries for @bo (cayman+).
  962. * Returns 0 for success, -EINVAL for failure.
  963. *
  964. * Object have to be reserved & global and local mutex must be locked!
  965. */
  966. int radeon_vm_bo_update_pte(struct radeon_device *rdev,
  967. struct radeon_vm *vm,
  968. struct radeon_bo *bo,
  969. struct ttm_mem_reg *mem)
  970. {
  971. unsigned ridx = rdev->asic->vm.pt_ring_index;
  972. struct radeon_ib ib;
  973. struct radeon_bo_va *bo_va;
  974. unsigned nptes, npdes, ndw;
  975. uint64_t addr;
  976. int r;
  977. /* nothing to do if vm isn't bound */
  978. if (vm->page_directory == NULL)
  979. return 0;
  980. bo_va = radeon_vm_bo_find(vm, bo);
  981. if (bo_va == NULL) {
  982. dev_err(rdev->dev, "bo %p not in vm %p\n", bo, vm);
  983. return -EINVAL;
  984. }
  985. if (!bo_va->soffset) {
  986. dev_err(rdev->dev, "bo %p don't has a mapping in vm %p\n",
  987. bo, vm);
  988. return -EINVAL;
  989. }
  990. if ((bo_va->valid && mem) || (!bo_va->valid && mem == NULL))
  991. return 0;
  992. bo_va->flags &= ~RADEON_VM_PAGE_VALID;
  993. bo_va->flags &= ~RADEON_VM_PAGE_SYSTEM;
  994. if (mem) {
  995. addr = mem->start << PAGE_SHIFT;
  996. if (mem->mem_type != TTM_PL_SYSTEM) {
  997. bo_va->flags |= RADEON_VM_PAGE_VALID;
  998. bo_va->valid = true;
  999. }
  1000. if (mem->mem_type == TTM_PL_TT) {
  1001. bo_va->flags |= RADEON_VM_PAGE_SYSTEM;
  1002. } else {
  1003. addr += rdev->vm_manager.vram_base_offset;
  1004. }
  1005. } else {
  1006. addr = 0;
  1007. bo_va->valid = false;
  1008. }
  1009. nptes = radeon_bo_ngpu_pages(bo);
  1010. /* assume two extra pdes in case the mapping overlaps the borders */
  1011. npdes = (nptes >> RADEON_VM_BLOCK_SIZE) + 2;
  1012. /* padding, etc. */
  1013. ndw = 64;
  1014. if (RADEON_VM_BLOCK_SIZE > 11)
  1015. /* reserve space for one header for every 2k dwords */
  1016. ndw += (nptes >> 11) * 4;
  1017. else
  1018. /* reserve space for one header for
  1019. every (1 << BLOCK_SIZE) entries */
  1020. ndw += (nptes >> RADEON_VM_BLOCK_SIZE) * 4;
  1021. /* reserve space for pte addresses */
  1022. ndw += nptes * 2;
  1023. /* reserve space for one header for every 2k dwords */
  1024. ndw += (npdes >> 11) * 4;
  1025. /* reserve space for pde addresses */
  1026. ndw += npdes * 2;
  1027. /* update too big for an IB */
  1028. if (ndw > 0xfffff)
  1029. return -ENOMEM;
  1030. r = radeon_ib_get(rdev, ridx, &ib, NULL, ndw * 4);
  1031. ib.length_dw = 0;
  1032. r = radeon_vm_update_pdes(rdev, vm, &ib, bo_va->soffset, bo_va->eoffset);
  1033. if (r) {
  1034. radeon_ib_free(rdev, &ib);
  1035. return r;
  1036. }
  1037. radeon_vm_update_ptes(rdev, vm, &ib, bo_va->soffset, bo_va->eoffset,
  1038. addr, bo_va->flags);
  1039. radeon_ib_sync_to(&ib, vm->fence);
  1040. r = radeon_ib_schedule(rdev, &ib, NULL);
  1041. if (r) {
  1042. radeon_ib_free(rdev, &ib);
  1043. return r;
  1044. }
  1045. radeon_fence_unref(&vm->fence);
  1046. vm->fence = radeon_fence_ref(ib.fence);
  1047. radeon_ib_free(rdev, &ib);
  1048. radeon_fence_unref(&vm->last_flush);
  1049. return 0;
  1050. }
  1051. /**
  1052. * radeon_vm_bo_rmv - remove a bo to a specific vm
  1053. *
  1054. * @rdev: radeon_device pointer
  1055. * @bo_va: requested bo_va
  1056. *
  1057. * Remove @bo_va->bo from the requested vm (cayman+).
  1058. * Remove @bo_va->bo from the list of bos associated with the bo_va->vm and
  1059. * remove the ptes for @bo_va in the page table.
  1060. * Returns 0 for success.
  1061. *
  1062. * Object have to be reserved!
  1063. */
  1064. int radeon_vm_bo_rmv(struct radeon_device *rdev,
  1065. struct radeon_bo_va *bo_va)
  1066. {
  1067. int r = 0;
  1068. mutex_lock(&rdev->vm_manager.lock);
  1069. mutex_lock(&bo_va->vm->mutex);
  1070. if (bo_va->soffset) {
  1071. r = radeon_vm_bo_update_pte(rdev, bo_va->vm, bo_va->bo, NULL);
  1072. }
  1073. mutex_unlock(&rdev->vm_manager.lock);
  1074. list_del(&bo_va->vm_list);
  1075. mutex_unlock(&bo_va->vm->mutex);
  1076. list_del(&bo_va->bo_list);
  1077. kfree(bo_va);
  1078. return r;
  1079. }
  1080. /**
  1081. * radeon_vm_bo_invalidate - mark the bo as invalid
  1082. *
  1083. * @rdev: radeon_device pointer
  1084. * @vm: requested vm
  1085. * @bo: radeon buffer object
  1086. *
  1087. * Mark @bo as invalid (cayman+).
  1088. */
  1089. void radeon_vm_bo_invalidate(struct radeon_device *rdev,
  1090. struct radeon_bo *bo)
  1091. {
  1092. struct radeon_bo_va *bo_va;
  1093. list_for_each_entry(bo_va, &bo->va, bo_list) {
  1094. bo_va->valid = false;
  1095. }
  1096. }
  1097. /**
  1098. * radeon_vm_init - initialize a vm instance
  1099. *
  1100. * @rdev: radeon_device pointer
  1101. * @vm: requested vm
  1102. *
  1103. * Init @vm fields (cayman+).
  1104. */
  1105. void radeon_vm_init(struct radeon_device *rdev, struct radeon_vm *vm)
  1106. {
  1107. vm->id = 0;
  1108. vm->fence = NULL;
  1109. mutex_init(&vm->mutex);
  1110. INIT_LIST_HEAD(&vm->list);
  1111. INIT_LIST_HEAD(&vm->va);
  1112. }
  1113. /**
  1114. * radeon_vm_fini - tear down a vm instance
  1115. *
  1116. * @rdev: radeon_device pointer
  1117. * @vm: requested vm
  1118. *
  1119. * Tear down @vm (cayman+).
  1120. * Unbind the VM and remove all bos from the vm bo list
  1121. */
  1122. void radeon_vm_fini(struct radeon_device *rdev, struct radeon_vm *vm)
  1123. {
  1124. struct radeon_bo_va *bo_va, *tmp;
  1125. int r;
  1126. mutex_lock(&rdev->vm_manager.lock);
  1127. mutex_lock(&vm->mutex);
  1128. radeon_vm_free_pt(rdev, vm);
  1129. mutex_unlock(&rdev->vm_manager.lock);
  1130. if (!list_empty(&vm->va)) {
  1131. dev_err(rdev->dev, "still active bo inside vm\n");
  1132. }
  1133. list_for_each_entry_safe(bo_va, tmp, &vm->va, vm_list) {
  1134. list_del_init(&bo_va->vm_list);
  1135. r = radeon_bo_reserve(bo_va->bo, false);
  1136. if (!r) {
  1137. list_del_init(&bo_va->bo_list);
  1138. radeon_bo_unreserve(bo_va->bo);
  1139. kfree(bo_va);
  1140. }
  1141. }
  1142. radeon_fence_unref(&vm->fence);
  1143. radeon_fence_unref(&vm->last_flush);
  1144. mutex_unlock(&vm->mutex);
  1145. }