amdgpu_vm.c 35 KB

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