amdgpu_vm.c 38 KB

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