amdgpu_vm.c 38 KB

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