amdgpu_vm.c 34 KB

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