amdgpu_vm.c 34 KB

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