amdgpu_vm.c 36 KB

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