amdgpu_vm.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  1. /*
  2. * Copyright 2008 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. * Copyright 2009 Jerome Glisse.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors: Dave Airlie
  25. * Alex Deucher
  26. * Jerome Glisse
  27. */
  28. #include <drm/drmP.h>
  29. #include <drm/amdgpu_drm.h>
  30. #include "amdgpu.h"
  31. #include "amdgpu_trace.h"
  32. /*
  33. * GPUVM
  34. * GPUVM is similar to the legacy gart on older asics, however
  35. * rather than there being a single global gart table
  36. * for the entire GPU, there are multiple VM page tables active
  37. * at any given time. The VM page tables can contain a mix
  38. * vram pages and system memory pages and system memory pages
  39. * can be mapped as snooped (cached system pages) or unsnooped
  40. * (uncached system pages).
  41. * Each VM has an ID associated with it and there is a page table
  42. * associated with each VMID. When execting a command buffer,
  43. * the kernel tells the the ring what VMID to use for that command
  44. * buffer. VMIDs are allocated dynamically as commands are submitted.
  45. * The userspace drivers maintain their own address space and the kernel
  46. * sets up their pages tables accordingly when they submit their
  47. * command buffers and a VMID is assigned.
  48. * Cayman/Trinity support up to 8 active VMs at any given time;
  49. * SI supports 16.
  50. */
  51. /**
  52. * amdgpu_vm_num_pde - return the number of page directory entries
  53. *
  54. * @adev: amdgpu_device pointer
  55. *
  56. * Calculate the number of page directory entries (cayman+).
  57. */
  58. static unsigned amdgpu_vm_num_pdes(struct amdgpu_device *adev)
  59. {
  60. return adev->vm_manager.max_pfn >> amdgpu_vm_block_size;
  61. }
  62. /**
  63. * amdgpu_vm_directory_size - returns the size of the page directory in bytes
  64. *
  65. * @adev: amdgpu_device pointer
  66. *
  67. * Calculate the size of the page directory in bytes (cayman+).
  68. */
  69. static unsigned amdgpu_vm_directory_size(struct amdgpu_device *adev)
  70. {
  71. return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_pdes(adev) * 8);
  72. }
  73. /**
  74. * amdgpu_vm_get_bos - add the vm BOs to a validation list
  75. *
  76. * @vm: vm providing the BOs
  77. * @head: head of validation list
  78. *
  79. * Add the page directory to the list of BOs to
  80. * validate for command submission (cayman+).
  81. */
  82. struct amdgpu_bo_list_entry *amdgpu_vm_get_bos(struct amdgpu_device *adev,
  83. struct amdgpu_vm *vm,
  84. struct list_head *head)
  85. {
  86. struct amdgpu_bo_list_entry *list;
  87. unsigned i, idx;
  88. list = drm_malloc_ab(vm->max_pde_used + 2,
  89. sizeof(struct amdgpu_bo_list_entry));
  90. if (!list) {
  91. return NULL;
  92. }
  93. /* add the vm page table to the list */
  94. list[0].robj = vm->page_directory;
  95. list[0].prefered_domains = AMDGPU_GEM_DOMAIN_VRAM;
  96. list[0].allowed_domains = AMDGPU_GEM_DOMAIN_VRAM;
  97. list[0].priority = 0;
  98. list[0].tv.bo = &vm->page_directory->tbo;
  99. list[0].tv.shared = true;
  100. list_add(&list[0].tv.head, head);
  101. for (i = 0, idx = 1; i <= vm->max_pde_used; i++) {
  102. if (!vm->page_tables[i].bo)
  103. continue;
  104. list[idx].robj = vm->page_tables[i].bo;
  105. list[idx].prefered_domains = AMDGPU_GEM_DOMAIN_VRAM;
  106. list[idx].allowed_domains = AMDGPU_GEM_DOMAIN_VRAM;
  107. list[idx].priority = 0;
  108. list[idx].tv.bo = &list[idx].robj->tbo;
  109. list[idx].tv.shared = true;
  110. list_add(&list[idx++].tv.head, head);
  111. }
  112. return list;
  113. }
  114. /**
  115. * amdgpu_vm_grab_id - allocate the next free VMID
  116. *
  117. * @vm: vm to allocate id for
  118. * @ring: ring we want to submit job to
  119. * @sync: sync object where we add dependencies
  120. *
  121. * Allocate an id for the vm, adding fences to the sync obj as necessary.
  122. *
  123. * Global mutex must be locked!
  124. */
  125. int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
  126. struct amdgpu_sync *sync)
  127. {
  128. struct amdgpu_fence *best[AMDGPU_MAX_RINGS] = {};
  129. struct amdgpu_vm_id *vm_id = &vm->ids[ring->idx];
  130. struct amdgpu_device *adev = ring->adev;
  131. unsigned choices[2] = {};
  132. unsigned i;
  133. /* check if the id is still valid */
  134. if (vm_id->id && vm_id->last_id_use &&
  135. vm_id->last_id_use == adev->vm_manager.active[vm_id->id]) {
  136. trace_amdgpu_vm_grab_id(vm_id->id, ring->idx);
  137. return 0;
  138. }
  139. /* we definately need to flush */
  140. vm_id->pd_gpu_addr = ~0ll;
  141. /* skip over VMID 0, since it is the system VM */
  142. for (i = 1; i < adev->vm_manager.nvm; ++i) {
  143. struct amdgpu_fence *fence = adev->vm_manager.active[i];
  144. if (fence == NULL) {
  145. /* found a free one */
  146. vm_id->id = i;
  147. trace_amdgpu_vm_grab_id(i, ring->idx);
  148. return 0;
  149. }
  150. if (amdgpu_fence_is_earlier(fence, best[fence->ring->idx])) {
  151. best[fence->ring->idx] = fence;
  152. choices[fence->ring == ring ? 0 : 1] = i;
  153. }
  154. }
  155. for (i = 0; i < 2; ++i) {
  156. if (choices[i]) {
  157. struct amdgpu_fence *fence;
  158. fence = adev->vm_manager.active[choices[i]];
  159. vm_id->id = choices[i];
  160. trace_amdgpu_vm_grab_id(choices[i], ring->idx);
  161. return amdgpu_sync_fence(ring->adev, sync, &fence->base);
  162. }
  163. }
  164. /* should never happen */
  165. BUG();
  166. return -EINVAL;
  167. }
  168. /**
  169. * amdgpu_vm_flush - hardware flush the vm
  170. *
  171. * @ring: ring to use for flush
  172. * @vm: vm we want to flush
  173. * @updates: last vm update that we waited for
  174. *
  175. * Flush the vm (cayman+).
  176. *
  177. * Global and local mutex must be locked!
  178. */
  179. void amdgpu_vm_flush(struct amdgpu_ring *ring,
  180. struct amdgpu_vm *vm,
  181. struct fence *updates)
  182. {
  183. uint64_t pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
  184. struct amdgpu_vm_id *vm_id = &vm->ids[ring->idx];
  185. struct fence *flushed_updates = vm_id->flushed_updates;
  186. bool is_earlier = false;
  187. if (flushed_updates && updates) {
  188. BUG_ON(flushed_updates->context != updates->context);
  189. is_earlier = (updates->seqno - flushed_updates->seqno <=
  190. INT_MAX) ? true : false;
  191. }
  192. if (pd_addr != vm_id->pd_gpu_addr || !flushed_updates ||
  193. is_earlier) {
  194. trace_amdgpu_vm_flush(pd_addr, ring->idx, vm_id->id);
  195. if (is_earlier) {
  196. vm_id->flushed_updates = fence_get(updates);
  197. fence_put(flushed_updates);
  198. }
  199. if (!flushed_updates)
  200. vm_id->flushed_updates = fence_get(updates);
  201. vm_id->pd_gpu_addr = pd_addr;
  202. amdgpu_ring_emit_vm_flush(ring, vm_id->id, vm_id->pd_gpu_addr);
  203. }
  204. }
  205. /**
  206. * amdgpu_vm_fence - remember fence for vm
  207. *
  208. * @adev: amdgpu_device pointer
  209. * @vm: vm we want to fence
  210. * @fence: fence to remember
  211. *
  212. * Fence the vm (cayman+).
  213. * Set the fence used to protect page table and id.
  214. *
  215. * Global and local mutex must be locked!
  216. */
  217. void amdgpu_vm_fence(struct amdgpu_device *adev,
  218. struct amdgpu_vm *vm,
  219. struct amdgpu_fence *fence)
  220. {
  221. unsigned ridx = fence->ring->idx;
  222. unsigned vm_id = vm->ids[ridx].id;
  223. amdgpu_fence_unref(&adev->vm_manager.active[vm_id]);
  224. adev->vm_manager.active[vm_id] = amdgpu_fence_ref(fence);
  225. amdgpu_fence_unref(&vm->ids[ridx].last_id_use);
  226. vm->ids[ridx].last_id_use = amdgpu_fence_ref(fence);
  227. }
  228. /**
  229. * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
  230. *
  231. * @vm: requested vm
  232. * @bo: requested buffer object
  233. *
  234. * Find @bo inside the requested vm (cayman+).
  235. * Search inside the @bos vm list for the requested vm
  236. * Returns the found bo_va or NULL if none is found
  237. *
  238. * Object has to be reserved!
  239. */
  240. struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
  241. struct amdgpu_bo *bo)
  242. {
  243. struct amdgpu_bo_va *bo_va;
  244. list_for_each_entry(bo_va, &bo->va, bo_list) {
  245. if (bo_va->vm == vm) {
  246. return bo_va;
  247. }
  248. }
  249. return NULL;
  250. }
  251. /**
  252. * amdgpu_vm_update_pages - helper to call the right asic function
  253. *
  254. * @adev: amdgpu_device pointer
  255. * @ib: indirect buffer to fill with commands
  256. * @pe: addr of the page entry
  257. * @addr: dst addr to write into pe
  258. * @count: number of page entries to update
  259. * @incr: increase next addr by incr bytes
  260. * @flags: hw access flags
  261. * @gtt_flags: GTT hw access flags
  262. *
  263. * Traces the parameters and calls the right asic functions
  264. * to setup the page table using the DMA.
  265. */
  266. static void amdgpu_vm_update_pages(struct amdgpu_device *adev,
  267. struct amdgpu_ib *ib,
  268. uint64_t pe, uint64_t addr,
  269. unsigned count, uint32_t incr,
  270. uint32_t flags, uint32_t gtt_flags)
  271. {
  272. trace_amdgpu_vm_set_page(pe, addr, count, incr, flags);
  273. if ((flags & AMDGPU_PTE_SYSTEM) && (flags == gtt_flags)) {
  274. uint64_t src = adev->gart.table_addr + (addr >> 12) * 8;
  275. amdgpu_vm_copy_pte(adev, ib, pe, src, count);
  276. } else if ((flags & AMDGPU_PTE_SYSTEM) || (count < 3)) {
  277. amdgpu_vm_write_pte(adev, ib, pe, addr,
  278. count, incr, flags);
  279. } else {
  280. amdgpu_vm_set_pte_pde(adev, ib, pe, addr,
  281. count, incr, flags);
  282. }
  283. }
  284. int amdgpu_vm_free_job(struct amdgpu_job *job)
  285. {
  286. int i;
  287. for (i = 0; i < job->num_ibs; i++)
  288. amdgpu_ib_free(job->adev, &job->ibs[i]);
  289. kfree(job->ibs);
  290. return 0;
  291. }
  292. /**
  293. * amdgpu_vm_clear_bo - initially clear the page dir/table
  294. *
  295. * @adev: amdgpu_device pointer
  296. * @bo: bo to clear
  297. */
  298. static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
  299. struct amdgpu_bo *bo)
  300. {
  301. struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
  302. struct fence *fence = NULL;
  303. struct amdgpu_ib *ib;
  304. unsigned entries;
  305. uint64_t addr;
  306. int r;
  307. r = amdgpu_bo_reserve(bo, false);
  308. if (r)
  309. return r;
  310. r = reservation_object_reserve_shared(bo->tbo.resv);
  311. if (r)
  312. return r;
  313. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  314. if (r)
  315. goto error_unreserve;
  316. addr = amdgpu_bo_gpu_offset(bo);
  317. entries = amdgpu_bo_size(bo) / 8;
  318. ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
  319. if (!ib)
  320. goto error_unreserve;
  321. r = amdgpu_ib_get(ring, NULL, entries * 2 + 64, ib);
  322. if (r)
  323. goto error_free;
  324. ib->length_dw = 0;
  325. amdgpu_vm_update_pages(adev, ib, addr, 0, entries, 0, 0, 0);
  326. amdgpu_vm_pad_ib(adev, ib);
  327. WARN_ON(ib->length_dw > 64);
  328. r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
  329. &amdgpu_vm_free_job,
  330. AMDGPU_FENCE_OWNER_VM,
  331. &fence);
  332. if (!r)
  333. amdgpu_bo_fence(bo, fence, true);
  334. fence_put(fence);
  335. if (amdgpu_enable_scheduler) {
  336. amdgpu_bo_unreserve(bo);
  337. return 0;
  338. }
  339. error_free:
  340. amdgpu_ib_free(adev, ib);
  341. kfree(ib);
  342. error_unreserve:
  343. amdgpu_bo_unreserve(bo);
  344. return r;
  345. }
  346. /**
  347. * amdgpu_vm_map_gart - get the physical address of a gart page
  348. *
  349. * @adev: amdgpu_device pointer
  350. * @addr: the unmapped addr
  351. *
  352. * Look up the physical address of the page that the pte resolves
  353. * to (cayman+).
  354. * Returns the physical address of the page.
  355. */
  356. uint64_t amdgpu_vm_map_gart(struct amdgpu_device *adev, uint64_t addr)
  357. {
  358. uint64_t result;
  359. /* page table offset */
  360. result = adev->gart.pages_addr[addr >> PAGE_SHIFT];
  361. /* in case cpu page size != gpu page size*/
  362. result |= addr & (~PAGE_MASK);
  363. return result;
  364. }
  365. /**
  366. * amdgpu_vm_update_pdes - make sure that page directory is valid
  367. *
  368. * @adev: amdgpu_device pointer
  369. * @vm: requested vm
  370. * @start: start of GPU address range
  371. * @end: end of GPU address range
  372. *
  373. * Allocates new page tables if necessary
  374. * and updates the page directory (cayman+).
  375. * Returns 0 for success, error for failure.
  376. *
  377. * Global and local mutex must be locked!
  378. */
  379. int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
  380. struct amdgpu_vm *vm)
  381. {
  382. struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
  383. struct amdgpu_bo *pd = vm->page_directory;
  384. uint64_t pd_addr = amdgpu_bo_gpu_offset(pd);
  385. uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
  386. uint64_t last_pde = ~0, last_pt = ~0;
  387. unsigned count = 0, pt_idx, ndw;
  388. struct amdgpu_ib *ib;
  389. struct fence *fence = NULL;
  390. int r;
  391. /* padding, etc. */
  392. ndw = 64;
  393. /* assume the worst case */
  394. ndw += vm->max_pde_used * 6;
  395. /* update too big for an IB */
  396. if (ndw > 0xfffff)
  397. return -ENOMEM;
  398. ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
  399. if (!ib)
  400. return -ENOMEM;
  401. r = amdgpu_ib_get(ring, NULL, ndw * 4, ib);
  402. if (r)
  403. return r;
  404. ib->length_dw = 0;
  405. /* walk over the address space and update the page directory */
  406. for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
  407. struct amdgpu_bo *bo = vm->page_tables[pt_idx].bo;
  408. uint64_t pde, pt;
  409. if (bo == NULL)
  410. continue;
  411. pt = amdgpu_bo_gpu_offset(bo);
  412. if (vm->page_tables[pt_idx].addr == pt)
  413. continue;
  414. vm->page_tables[pt_idx].addr = pt;
  415. pde = pd_addr + pt_idx * 8;
  416. if (((last_pde + 8 * count) != pde) ||
  417. ((last_pt + incr * count) != pt)) {
  418. if (count) {
  419. amdgpu_vm_update_pages(adev, ib, last_pde,
  420. last_pt, count, incr,
  421. AMDGPU_PTE_VALID, 0);
  422. }
  423. count = 1;
  424. last_pde = pde;
  425. last_pt = pt;
  426. } else {
  427. ++count;
  428. }
  429. }
  430. if (count)
  431. amdgpu_vm_update_pages(adev, ib, last_pde, last_pt, count,
  432. incr, AMDGPU_PTE_VALID, 0);
  433. if (ib->length_dw != 0) {
  434. amdgpu_vm_pad_ib(adev, ib);
  435. amdgpu_sync_resv(adev, &ib->sync, pd->tbo.resv, AMDGPU_FENCE_OWNER_VM);
  436. WARN_ON(ib->length_dw > ndw);
  437. r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
  438. &amdgpu_vm_free_job,
  439. AMDGPU_FENCE_OWNER_VM,
  440. &fence);
  441. if (r)
  442. goto error_free;
  443. amdgpu_bo_fence(pd, fence, true);
  444. fence_put(vm->page_directory_fence);
  445. vm->page_directory_fence = fence_get(fence);
  446. fence_put(fence);
  447. }
  448. if (!amdgpu_enable_scheduler || ib->length_dw == 0) {
  449. amdgpu_ib_free(adev, ib);
  450. kfree(ib);
  451. }
  452. return 0;
  453. error_free:
  454. amdgpu_ib_free(adev, ib);
  455. kfree(ib);
  456. return r;
  457. }
  458. /**
  459. * amdgpu_vm_frag_ptes - add fragment information to PTEs
  460. *
  461. * @adev: amdgpu_device pointer
  462. * @ib: IB for the update
  463. * @pe_start: first PTE to handle
  464. * @pe_end: last PTE to handle
  465. * @addr: addr those PTEs should point to
  466. * @flags: hw mapping flags
  467. * @gtt_flags: GTT hw mapping flags
  468. *
  469. * Global and local mutex must be locked!
  470. */
  471. static void amdgpu_vm_frag_ptes(struct amdgpu_device *adev,
  472. struct amdgpu_ib *ib,
  473. uint64_t pe_start, uint64_t pe_end,
  474. uint64_t addr, uint32_t flags,
  475. uint32_t gtt_flags)
  476. {
  477. /**
  478. * The MC L1 TLB supports variable sized pages, based on a fragment
  479. * field in the PTE. When this field is set to a non-zero value, page
  480. * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
  481. * flags are considered valid for all PTEs within the fragment range
  482. * and corresponding mappings are assumed to be physically contiguous.
  483. *
  484. * The L1 TLB can store a single PTE for the whole fragment,
  485. * significantly increasing the space available for translation
  486. * caching. This leads to large improvements in throughput when the
  487. * TLB is under pressure.
  488. *
  489. * The L2 TLB distributes small and large fragments into two
  490. * asymmetric partitions. The large fragment cache is significantly
  491. * larger. Thus, we try to use large fragments wherever possible.
  492. * Userspace can support this by aligning virtual base address and
  493. * allocation size to the fragment size.
  494. */
  495. /* SI and newer are optimized for 64KB */
  496. uint64_t frag_flags = AMDGPU_PTE_FRAG_64KB;
  497. uint64_t frag_align = 0x80;
  498. uint64_t frag_start = ALIGN(pe_start, frag_align);
  499. uint64_t frag_end = pe_end & ~(frag_align - 1);
  500. unsigned count;
  501. /* system pages are non continuously */
  502. if ((flags & AMDGPU_PTE_SYSTEM) || !(flags & AMDGPU_PTE_VALID) ||
  503. (frag_start >= frag_end)) {
  504. count = (pe_end - pe_start) / 8;
  505. amdgpu_vm_update_pages(adev, ib, pe_start, addr, count,
  506. AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags);
  507. return;
  508. }
  509. /* handle the 4K area at the beginning */
  510. if (pe_start != frag_start) {
  511. count = (frag_start - pe_start) / 8;
  512. amdgpu_vm_update_pages(adev, ib, pe_start, addr, count,
  513. AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags);
  514. addr += AMDGPU_GPU_PAGE_SIZE * count;
  515. }
  516. /* handle the area in the middle */
  517. count = (frag_end - frag_start) / 8;
  518. amdgpu_vm_update_pages(adev, ib, frag_start, addr, count,
  519. AMDGPU_GPU_PAGE_SIZE, flags | frag_flags,
  520. gtt_flags);
  521. /* handle the 4K area at the end */
  522. if (frag_end != pe_end) {
  523. addr += AMDGPU_GPU_PAGE_SIZE * count;
  524. count = (pe_end - frag_end) / 8;
  525. amdgpu_vm_update_pages(adev, ib, frag_end, addr, count,
  526. AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags);
  527. }
  528. }
  529. /**
  530. * amdgpu_vm_update_ptes - make sure that page tables are valid
  531. *
  532. * @adev: amdgpu_device pointer
  533. * @vm: requested vm
  534. * @start: start of GPU address range
  535. * @end: end of GPU address range
  536. * @dst: destination address to map to
  537. * @flags: mapping flags
  538. *
  539. * Update the page tables in the range @start - @end (cayman+).
  540. *
  541. * Global and local mutex must be locked!
  542. */
  543. static int amdgpu_vm_update_ptes(struct amdgpu_device *adev,
  544. struct amdgpu_vm *vm,
  545. struct amdgpu_ib *ib,
  546. uint64_t start, uint64_t end,
  547. uint64_t dst, uint32_t flags,
  548. uint32_t gtt_flags)
  549. {
  550. uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
  551. uint64_t last_pte = ~0, last_dst = ~0;
  552. void *owner = AMDGPU_FENCE_OWNER_VM;
  553. unsigned count = 0;
  554. uint64_t addr;
  555. /* sync to everything on unmapping */
  556. if (!(flags & AMDGPU_PTE_VALID))
  557. owner = AMDGPU_FENCE_OWNER_UNDEFINED;
  558. /* walk over the address space and update the page tables */
  559. for (addr = start; addr < end; ) {
  560. uint64_t pt_idx = addr >> amdgpu_vm_block_size;
  561. struct amdgpu_bo *pt = vm->page_tables[pt_idx].bo;
  562. unsigned nptes;
  563. uint64_t pte;
  564. int r;
  565. amdgpu_sync_resv(adev, &ib->sync, pt->tbo.resv, owner);
  566. r = reservation_object_reserve_shared(pt->tbo.resv);
  567. if (r)
  568. return r;
  569. if ((addr & ~mask) == (end & ~mask))
  570. nptes = end - addr;
  571. else
  572. nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
  573. pte = amdgpu_bo_gpu_offset(pt);
  574. pte += (addr & mask) * 8;
  575. if ((last_pte + 8 * count) != pte) {
  576. if (count) {
  577. amdgpu_vm_frag_ptes(adev, ib, last_pte,
  578. last_pte + 8 * count,
  579. last_dst, flags,
  580. gtt_flags);
  581. }
  582. count = nptes;
  583. last_pte = pte;
  584. last_dst = dst;
  585. } else {
  586. count += nptes;
  587. }
  588. addr += nptes;
  589. dst += nptes * AMDGPU_GPU_PAGE_SIZE;
  590. }
  591. if (count) {
  592. amdgpu_vm_frag_ptes(adev, ib, last_pte,
  593. last_pte + 8 * count,
  594. last_dst, flags, gtt_flags);
  595. }
  596. return 0;
  597. }
  598. /**
  599. * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
  600. *
  601. * @adev: amdgpu_device pointer
  602. * @vm: requested vm
  603. * @mapping: mapped range and flags to use for the update
  604. * @addr: addr to set the area to
  605. * @gtt_flags: flags as they are used for GTT
  606. * @fence: optional resulting fence
  607. *
  608. * Fill in the page table entries for @mapping.
  609. * Returns 0 for success, -EINVAL for failure.
  610. *
  611. * Object have to be reserved and mutex must be locked!
  612. */
  613. static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
  614. struct amdgpu_vm *vm,
  615. struct amdgpu_bo_va_mapping *mapping,
  616. uint64_t addr, uint32_t gtt_flags,
  617. struct fence **fence)
  618. {
  619. struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
  620. unsigned nptes, ncmds, ndw;
  621. uint32_t flags = gtt_flags;
  622. struct amdgpu_ib *ib;
  623. struct fence *f = NULL;
  624. int r;
  625. /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
  626. * but in case of something, we filter the flags in first place
  627. */
  628. if (!(mapping->flags & AMDGPU_PTE_READABLE))
  629. flags &= ~AMDGPU_PTE_READABLE;
  630. if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
  631. flags &= ~AMDGPU_PTE_WRITEABLE;
  632. trace_amdgpu_vm_bo_update(mapping);
  633. nptes = mapping->it.last - mapping->it.start + 1;
  634. /*
  635. * reserve space for one command every (1 << BLOCK_SIZE)
  636. * entries or 2k dwords (whatever is smaller)
  637. */
  638. ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
  639. /* padding, etc. */
  640. ndw = 64;
  641. if ((flags & AMDGPU_PTE_SYSTEM) && (flags == gtt_flags)) {
  642. /* only copy commands needed */
  643. ndw += ncmds * 7;
  644. } else if (flags & AMDGPU_PTE_SYSTEM) {
  645. /* header for write data commands */
  646. ndw += ncmds * 4;
  647. /* body of write data command */
  648. ndw += nptes * 2;
  649. } else {
  650. /* set page commands needed */
  651. ndw += ncmds * 10;
  652. /* two extra commands for begin/end of fragment */
  653. ndw += 2 * 10;
  654. }
  655. /* update too big for an IB */
  656. if (ndw > 0xfffff)
  657. return -ENOMEM;
  658. ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
  659. if (!ib)
  660. return -ENOMEM;
  661. r = amdgpu_ib_get(ring, NULL, ndw * 4, ib);
  662. if (r) {
  663. kfree(ib);
  664. return r;
  665. }
  666. ib->length_dw = 0;
  667. r = amdgpu_vm_update_ptes(adev, vm, ib, mapping->it.start,
  668. mapping->it.last + 1, addr + mapping->offset,
  669. flags, gtt_flags);
  670. if (r) {
  671. amdgpu_ib_free(adev, ib);
  672. kfree(ib);
  673. return r;
  674. }
  675. amdgpu_vm_pad_ib(adev, ib);
  676. WARN_ON(ib->length_dw > ndw);
  677. r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
  678. &amdgpu_vm_free_job,
  679. AMDGPU_FENCE_OWNER_VM,
  680. &f);
  681. if (r)
  682. goto error_free;
  683. amdgpu_bo_fence(vm->page_directory, f, true);
  684. if (fence) {
  685. fence_put(*fence);
  686. *fence = fence_get(f);
  687. }
  688. fence_put(f);
  689. if (!amdgpu_enable_scheduler) {
  690. amdgpu_ib_free(adev, ib);
  691. kfree(ib);
  692. }
  693. return 0;
  694. error_free:
  695. amdgpu_ib_free(adev, ib);
  696. kfree(ib);
  697. return r;
  698. }
  699. /**
  700. * amdgpu_vm_bo_update - update all BO mappings in the vm page table
  701. *
  702. * @adev: amdgpu_device pointer
  703. * @bo_va: requested BO and VM object
  704. * @mem: ttm mem
  705. *
  706. * Fill in the page table entries for @bo_va.
  707. * Returns 0 for success, -EINVAL for failure.
  708. *
  709. * Object have to be reserved and mutex must be locked!
  710. */
  711. int amdgpu_vm_bo_update(struct amdgpu_device *adev,
  712. struct amdgpu_bo_va *bo_va,
  713. struct ttm_mem_reg *mem)
  714. {
  715. struct amdgpu_vm *vm = bo_va->vm;
  716. struct amdgpu_bo_va_mapping *mapping;
  717. uint32_t flags;
  718. uint64_t addr;
  719. int r;
  720. if (mem) {
  721. addr = (u64)mem->start << PAGE_SHIFT;
  722. if (mem->mem_type != TTM_PL_TT)
  723. addr += adev->vm_manager.vram_base_offset;
  724. } else {
  725. addr = 0;
  726. }
  727. flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
  728. spin_lock(&vm->status_lock);
  729. if (!list_empty(&bo_va->vm_status))
  730. list_splice_init(&bo_va->valids, &bo_va->invalids);
  731. spin_unlock(&vm->status_lock);
  732. list_for_each_entry(mapping, &bo_va->invalids, list) {
  733. r = amdgpu_vm_bo_update_mapping(adev, vm, mapping, addr,
  734. flags, &bo_va->last_pt_update);
  735. if (r)
  736. return r;
  737. }
  738. if (trace_amdgpu_vm_bo_mapping_enabled()) {
  739. list_for_each_entry(mapping, &bo_va->valids, list)
  740. trace_amdgpu_vm_bo_mapping(mapping);
  741. list_for_each_entry(mapping, &bo_va->invalids, list)
  742. trace_amdgpu_vm_bo_mapping(mapping);
  743. }
  744. spin_lock(&vm->status_lock);
  745. list_splice_init(&bo_va->invalids, &bo_va->valids);
  746. list_del_init(&bo_va->vm_status);
  747. if (!mem)
  748. list_add(&bo_va->vm_status, &vm->cleared);
  749. spin_unlock(&vm->status_lock);
  750. return 0;
  751. }
  752. /**
  753. * amdgpu_vm_clear_freed - clear freed BOs in the PT
  754. *
  755. * @adev: amdgpu_device pointer
  756. * @vm: requested vm
  757. *
  758. * Make sure all freed BOs are cleared in the PT.
  759. * Returns 0 for success.
  760. *
  761. * PTs have to be reserved and mutex must be locked!
  762. */
  763. int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
  764. struct amdgpu_vm *vm)
  765. {
  766. struct amdgpu_bo_va_mapping *mapping;
  767. int r;
  768. while (!list_empty(&vm->freed)) {
  769. mapping = list_first_entry(&vm->freed,
  770. struct amdgpu_bo_va_mapping, list);
  771. list_del(&mapping->list);
  772. r = amdgpu_vm_bo_update_mapping(adev, vm, mapping, 0, 0, NULL);
  773. kfree(mapping);
  774. if (r)
  775. return r;
  776. }
  777. return 0;
  778. }
  779. /**
  780. * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
  781. *
  782. * @adev: amdgpu_device pointer
  783. * @vm: requested vm
  784. *
  785. * Make sure all invalidated BOs are cleared in the PT.
  786. * Returns 0 for success.
  787. *
  788. * PTs have to be reserved and mutex must be locked!
  789. */
  790. int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
  791. struct amdgpu_vm *vm, struct amdgpu_sync *sync)
  792. {
  793. struct amdgpu_bo_va *bo_va = NULL;
  794. int r = 0;
  795. spin_lock(&vm->status_lock);
  796. while (!list_empty(&vm->invalidated)) {
  797. bo_va = list_first_entry(&vm->invalidated,
  798. struct amdgpu_bo_va, vm_status);
  799. spin_unlock(&vm->status_lock);
  800. r = amdgpu_vm_bo_update(adev, bo_va, NULL);
  801. if (r)
  802. return r;
  803. spin_lock(&vm->status_lock);
  804. }
  805. spin_unlock(&vm->status_lock);
  806. if (bo_va)
  807. r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
  808. return r;
  809. }
  810. /**
  811. * amdgpu_vm_bo_add - add a bo to a specific vm
  812. *
  813. * @adev: amdgpu_device pointer
  814. * @vm: requested vm
  815. * @bo: amdgpu buffer object
  816. *
  817. * Add @bo into the requested vm (cayman+).
  818. * Add @bo to the list of bos associated with the vm
  819. * Returns newly added bo_va or NULL for failure
  820. *
  821. * Object has to be reserved!
  822. */
  823. struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
  824. struct amdgpu_vm *vm,
  825. struct amdgpu_bo *bo)
  826. {
  827. struct amdgpu_bo_va *bo_va;
  828. bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
  829. if (bo_va == NULL) {
  830. return NULL;
  831. }
  832. bo_va->vm = vm;
  833. bo_va->bo = bo;
  834. bo_va->ref_count = 1;
  835. INIT_LIST_HEAD(&bo_va->bo_list);
  836. INIT_LIST_HEAD(&bo_va->valids);
  837. INIT_LIST_HEAD(&bo_va->invalids);
  838. INIT_LIST_HEAD(&bo_va->vm_status);
  839. list_add_tail(&bo_va->bo_list, &bo->va);
  840. return bo_va;
  841. }
  842. /**
  843. * amdgpu_vm_bo_map - map bo inside a vm
  844. *
  845. * @adev: amdgpu_device pointer
  846. * @bo_va: bo_va to store the address
  847. * @saddr: where to map the BO
  848. * @offset: requested offset in the BO
  849. * @flags: attributes of pages (read/write/valid/etc.)
  850. *
  851. * Add a mapping of the BO at the specefied addr into the VM.
  852. * Returns 0 for success, error for failure.
  853. *
  854. * Object has to be reserved and gets unreserved by this function!
  855. */
  856. int amdgpu_vm_bo_map(struct amdgpu_device *adev,
  857. struct amdgpu_bo_va *bo_va,
  858. uint64_t saddr, uint64_t offset,
  859. uint64_t size, uint32_t flags)
  860. {
  861. struct amdgpu_bo_va_mapping *mapping;
  862. struct amdgpu_vm *vm = bo_va->vm;
  863. struct interval_tree_node *it;
  864. unsigned last_pfn, pt_idx;
  865. uint64_t eaddr;
  866. int r;
  867. /* validate the parameters */
  868. if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
  869. size == 0 || size & AMDGPU_GPU_PAGE_MASK) {
  870. amdgpu_bo_unreserve(bo_va->bo);
  871. return -EINVAL;
  872. }
  873. /* make sure object fit at this offset */
  874. eaddr = saddr + size;
  875. if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo))) {
  876. amdgpu_bo_unreserve(bo_va->bo);
  877. return -EINVAL;
  878. }
  879. last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
  880. if (last_pfn > adev->vm_manager.max_pfn) {
  881. dev_err(adev->dev, "va above limit (0x%08X > 0x%08X)\n",
  882. last_pfn, adev->vm_manager.max_pfn);
  883. amdgpu_bo_unreserve(bo_va->bo);
  884. return -EINVAL;
  885. }
  886. saddr /= AMDGPU_GPU_PAGE_SIZE;
  887. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  888. it = interval_tree_iter_first(&vm->va, saddr, eaddr - 1);
  889. if (it) {
  890. struct amdgpu_bo_va_mapping *tmp;
  891. tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
  892. /* bo and tmp overlap, invalid addr */
  893. dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
  894. "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
  895. tmp->it.start, tmp->it.last + 1);
  896. amdgpu_bo_unreserve(bo_va->bo);
  897. r = -EINVAL;
  898. goto error;
  899. }
  900. mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
  901. if (!mapping) {
  902. amdgpu_bo_unreserve(bo_va->bo);
  903. r = -ENOMEM;
  904. goto error;
  905. }
  906. INIT_LIST_HEAD(&mapping->list);
  907. mapping->it.start = saddr;
  908. mapping->it.last = eaddr - 1;
  909. mapping->offset = offset;
  910. mapping->flags = flags;
  911. list_add(&mapping->list, &bo_va->invalids);
  912. interval_tree_insert(&mapping->it, &vm->va);
  913. trace_amdgpu_vm_bo_map(bo_va, mapping);
  914. /* Make sure the page tables are allocated */
  915. saddr >>= amdgpu_vm_block_size;
  916. eaddr >>= amdgpu_vm_block_size;
  917. BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
  918. if (eaddr > vm->max_pde_used)
  919. vm->max_pde_used = eaddr;
  920. amdgpu_bo_unreserve(bo_va->bo);
  921. /* walk over the address space and allocate the page tables */
  922. for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
  923. struct reservation_object *resv = vm->page_directory->tbo.resv;
  924. struct amdgpu_bo *pt;
  925. if (vm->page_tables[pt_idx].bo)
  926. continue;
  927. ww_mutex_lock(&resv->lock, NULL);
  928. r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
  929. AMDGPU_GPU_PAGE_SIZE, true,
  930. AMDGPU_GEM_DOMAIN_VRAM,
  931. AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
  932. NULL, resv, &pt);
  933. ww_mutex_unlock(&resv->lock);
  934. if (r)
  935. goto error_free;
  936. r = amdgpu_vm_clear_bo(adev, pt);
  937. if (r) {
  938. amdgpu_bo_unref(&pt);
  939. goto error_free;
  940. }
  941. vm->page_tables[pt_idx].addr = 0;
  942. vm->page_tables[pt_idx].bo = pt;
  943. }
  944. return 0;
  945. error_free:
  946. list_del(&mapping->list);
  947. interval_tree_remove(&mapping->it, &vm->va);
  948. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  949. kfree(mapping);
  950. error:
  951. return r;
  952. }
  953. /**
  954. * amdgpu_vm_bo_unmap - remove bo mapping from vm
  955. *
  956. * @adev: amdgpu_device pointer
  957. * @bo_va: bo_va to remove the address from
  958. * @saddr: where to the BO is mapped
  959. *
  960. * Remove a mapping of the BO at the specefied addr from the VM.
  961. * Returns 0 for success, error for failure.
  962. *
  963. * Object has to be reserved and gets unreserved by this function!
  964. */
  965. int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
  966. struct amdgpu_bo_va *bo_va,
  967. uint64_t saddr)
  968. {
  969. struct amdgpu_bo_va_mapping *mapping;
  970. struct amdgpu_vm *vm = bo_va->vm;
  971. bool valid = true;
  972. saddr /= AMDGPU_GPU_PAGE_SIZE;
  973. list_for_each_entry(mapping, &bo_va->valids, list) {
  974. if (mapping->it.start == saddr)
  975. break;
  976. }
  977. if (&mapping->list == &bo_va->valids) {
  978. valid = false;
  979. list_for_each_entry(mapping, &bo_va->invalids, list) {
  980. if (mapping->it.start == saddr)
  981. break;
  982. }
  983. if (&mapping->list == &bo_va->invalids) {
  984. amdgpu_bo_unreserve(bo_va->bo);
  985. return -ENOENT;
  986. }
  987. }
  988. list_del(&mapping->list);
  989. interval_tree_remove(&mapping->it, &vm->va);
  990. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  991. if (valid)
  992. list_add(&mapping->list, &vm->freed);
  993. else
  994. kfree(mapping);
  995. amdgpu_bo_unreserve(bo_va->bo);
  996. return 0;
  997. }
  998. /**
  999. * amdgpu_vm_bo_rmv - remove a bo to a specific vm
  1000. *
  1001. * @adev: amdgpu_device pointer
  1002. * @bo_va: requested bo_va
  1003. *
  1004. * Remove @bo_va->bo from the requested vm (cayman+).
  1005. *
  1006. * Object have to be reserved!
  1007. */
  1008. void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
  1009. struct amdgpu_bo_va *bo_va)
  1010. {
  1011. struct amdgpu_bo_va_mapping *mapping, *next;
  1012. struct amdgpu_vm *vm = bo_va->vm;
  1013. list_del(&bo_va->bo_list);
  1014. spin_lock(&vm->status_lock);
  1015. list_del(&bo_va->vm_status);
  1016. spin_unlock(&vm->status_lock);
  1017. list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
  1018. list_del(&mapping->list);
  1019. interval_tree_remove(&mapping->it, &vm->va);
  1020. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  1021. list_add(&mapping->list, &vm->freed);
  1022. }
  1023. list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
  1024. list_del(&mapping->list);
  1025. interval_tree_remove(&mapping->it, &vm->va);
  1026. kfree(mapping);
  1027. }
  1028. fence_put(bo_va->last_pt_update);
  1029. kfree(bo_va);
  1030. }
  1031. /**
  1032. * amdgpu_vm_bo_invalidate - mark the bo as invalid
  1033. *
  1034. * @adev: amdgpu_device pointer
  1035. * @vm: requested vm
  1036. * @bo: amdgpu buffer object
  1037. *
  1038. * Mark @bo as invalid (cayman+).
  1039. */
  1040. void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
  1041. struct amdgpu_bo *bo)
  1042. {
  1043. struct amdgpu_bo_va *bo_va;
  1044. list_for_each_entry(bo_va, &bo->va, bo_list) {
  1045. spin_lock(&bo_va->vm->status_lock);
  1046. if (list_empty(&bo_va->vm_status))
  1047. list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
  1048. spin_unlock(&bo_va->vm->status_lock);
  1049. }
  1050. }
  1051. /**
  1052. * amdgpu_vm_init - initialize a vm instance
  1053. *
  1054. * @adev: amdgpu_device pointer
  1055. * @vm: requested vm
  1056. *
  1057. * Init @vm fields (cayman+).
  1058. */
  1059. int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  1060. {
  1061. const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
  1062. AMDGPU_VM_PTE_COUNT * 8);
  1063. unsigned pd_size, pd_entries, pts_size;
  1064. int i, r;
  1065. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  1066. vm->ids[i].id = 0;
  1067. vm->ids[i].flushed_updates = NULL;
  1068. vm->ids[i].last_id_use = NULL;
  1069. }
  1070. mutex_init(&vm->mutex);
  1071. vm->va = RB_ROOT;
  1072. spin_lock_init(&vm->status_lock);
  1073. INIT_LIST_HEAD(&vm->invalidated);
  1074. INIT_LIST_HEAD(&vm->cleared);
  1075. INIT_LIST_HEAD(&vm->freed);
  1076. pd_size = amdgpu_vm_directory_size(adev);
  1077. pd_entries = amdgpu_vm_num_pdes(adev);
  1078. /* allocate page table array */
  1079. pts_size = pd_entries * sizeof(struct amdgpu_vm_pt);
  1080. vm->page_tables = kzalloc(pts_size, GFP_KERNEL);
  1081. if (vm->page_tables == NULL) {
  1082. DRM_ERROR("Cannot allocate memory for page table array\n");
  1083. return -ENOMEM;
  1084. }
  1085. vm->page_directory_fence = NULL;
  1086. r = amdgpu_bo_create(adev, pd_size, align, true,
  1087. AMDGPU_GEM_DOMAIN_VRAM,
  1088. AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
  1089. NULL, NULL, &vm->page_directory);
  1090. if (r)
  1091. return r;
  1092. r = amdgpu_vm_clear_bo(adev, vm->page_directory);
  1093. if (r) {
  1094. amdgpu_bo_unref(&vm->page_directory);
  1095. vm->page_directory = NULL;
  1096. return r;
  1097. }
  1098. return 0;
  1099. }
  1100. /**
  1101. * amdgpu_vm_fini - tear down a vm instance
  1102. *
  1103. * @adev: amdgpu_device pointer
  1104. * @vm: requested vm
  1105. *
  1106. * Tear down @vm (cayman+).
  1107. * Unbind the VM and remove all bos from the vm bo list
  1108. */
  1109. void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  1110. {
  1111. struct amdgpu_bo_va_mapping *mapping, *tmp;
  1112. int i;
  1113. if (!RB_EMPTY_ROOT(&vm->va)) {
  1114. dev_err(adev->dev, "still active bo inside vm\n");
  1115. }
  1116. rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
  1117. list_del(&mapping->list);
  1118. interval_tree_remove(&mapping->it, &vm->va);
  1119. kfree(mapping);
  1120. }
  1121. list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
  1122. list_del(&mapping->list);
  1123. kfree(mapping);
  1124. }
  1125. for (i = 0; i < amdgpu_vm_num_pdes(adev); i++)
  1126. amdgpu_bo_unref(&vm->page_tables[i].bo);
  1127. kfree(vm->page_tables);
  1128. amdgpu_bo_unref(&vm->page_directory);
  1129. fence_put(vm->page_directory_fence);
  1130. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  1131. fence_put(vm->ids[i].flushed_updates);
  1132. amdgpu_fence_unref(&vm->ids[i].last_id_use);
  1133. }
  1134. mutex_destroy(&vm->mutex);
  1135. }