amdgpu_vm.c 34 KB

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