amdgpu_vm.c 34 KB

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