radeon_gart.c 33 KB

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