amdgpu_vm.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653
  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 <linux/dma-fence-array.h>
  29. #include <linux/interval_tree_generic.h>
  30. #include <linux/idr.h>
  31. #include <drm/drmP.h>
  32. #include <drm/amdgpu_drm.h>
  33. #include "amdgpu.h"
  34. #include "amdgpu_trace.h"
  35. /*
  36. * GPUVM
  37. * GPUVM is similar to the legacy gart on older asics, however
  38. * rather than there being a single global gart table
  39. * for the entire GPU, there are multiple VM page tables active
  40. * at any given time. The VM page tables can contain a mix
  41. * vram pages and system memory pages and system memory pages
  42. * can be mapped as snooped (cached system pages) or unsnooped
  43. * (uncached system pages).
  44. * Each VM has an ID associated with it and there is a page table
  45. * associated with each VMID. When execting a command buffer,
  46. * the kernel tells the the ring what VMID to use for that command
  47. * buffer. VMIDs are allocated dynamically as commands are submitted.
  48. * The userspace drivers maintain their own address space and the kernel
  49. * sets up their pages tables accordingly when they submit their
  50. * command buffers and a VMID is assigned.
  51. * Cayman/Trinity support up to 8 active VMs at any given time;
  52. * SI supports 16.
  53. */
  54. #define START(node) ((node)->start)
  55. #define LAST(node) ((node)->last)
  56. INTERVAL_TREE_DEFINE(struct amdgpu_bo_va_mapping, rb, uint64_t, __subtree_last,
  57. START, LAST, static, amdgpu_vm_it)
  58. #undef START
  59. #undef LAST
  60. /* Local structure. Encapsulate some VM table update parameters to reduce
  61. * the number of function parameters
  62. */
  63. struct amdgpu_pte_update_params {
  64. /* amdgpu device we do this update for */
  65. struct amdgpu_device *adev;
  66. /* optional amdgpu_vm we do this update for */
  67. struct amdgpu_vm *vm;
  68. /* address where to copy page table entries from */
  69. uint64_t src;
  70. /* indirect buffer to fill with commands */
  71. struct amdgpu_ib *ib;
  72. /* Function which actually does the update */
  73. void (*func)(struct amdgpu_pte_update_params *params,
  74. struct amdgpu_bo *bo, uint64_t pe,
  75. uint64_t addr, unsigned count, uint32_t incr,
  76. uint64_t flags);
  77. /* The next two are used during VM update by CPU
  78. * DMA addresses to use for mapping
  79. * Kernel pointer of PD/PT BO that needs to be updated
  80. */
  81. dma_addr_t *pages_addr;
  82. void *kptr;
  83. };
  84. /* Helper to disable partial resident texture feature from a fence callback */
  85. struct amdgpu_prt_cb {
  86. struct amdgpu_device *adev;
  87. struct dma_fence_cb cb;
  88. };
  89. /**
  90. * amdgpu_vm_level_shift - return the addr shift for each level
  91. *
  92. * @adev: amdgpu_device pointer
  93. *
  94. * Returns the number of bits the pfn needs to be right shifted for a level.
  95. */
  96. static unsigned amdgpu_vm_level_shift(struct amdgpu_device *adev,
  97. unsigned level)
  98. {
  99. unsigned shift = 0xff;
  100. switch (level) {
  101. case AMDGPU_VM_PDB2:
  102. case AMDGPU_VM_PDB1:
  103. case AMDGPU_VM_PDB0:
  104. shift = 9 * (AMDGPU_VM_PDB0 - level) +
  105. adev->vm_manager.block_size;
  106. break;
  107. case AMDGPU_VM_PTB:
  108. shift = 0;
  109. break;
  110. default:
  111. dev_err(adev->dev, "the level%d isn't supported.\n", level);
  112. }
  113. return shift;
  114. }
  115. /**
  116. * amdgpu_vm_num_entries - return the number of entries in a PD/PT
  117. *
  118. * @adev: amdgpu_device pointer
  119. *
  120. * Calculate the number of entries in a page directory or page table.
  121. */
  122. static unsigned amdgpu_vm_num_entries(struct amdgpu_device *adev,
  123. unsigned level)
  124. {
  125. unsigned shift = amdgpu_vm_level_shift(adev,
  126. adev->vm_manager.root_level);
  127. if (level == adev->vm_manager.root_level)
  128. /* For the root directory */
  129. return round_up(adev->vm_manager.max_pfn, 1 << shift) >> shift;
  130. else if (level != AMDGPU_VM_PTB)
  131. /* Everything in between */
  132. return 512;
  133. else
  134. /* For the page tables on the leaves */
  135. return AMDGPU_VM_PTE_COUNT(adev);
  136. }
  137. /**
  138. * amdgpu_vm_bo_size - returns the size of the BOs in bytes
  139. *
  140. * @adev: amdgpu_device pointer
  141. *
  142. * Calculate the size of the BO for a page directory or page table in bytes.
  143. */
  144. static unsigned amdgpu_vm_bo_size(struct amdgpu_device *adev, unsigned level)
  145. {
  146. return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev, level) * 8);
  147. }
  148. /**
  149. * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
  150. *
  151. * @vm: vm providing the BOs
  152. * @validated: head of validation list
  153. * @entry: entry to add
  154. *
  155. * Add the page directory to the list of BOs to
  156. * validate for command submission.
  157. */
  158. void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
  159. struct list_head *validated,
  160. struct amdgpu_bo_list_entry *entry)
  161. {
  162. entry->robj = vm->root.base.bo;
  163. entry->priority = 0;
  164. entry->tv.bo = &entry->robj->tbo;
  165. entry->tv.shared = true;
  166. entry->user_pages = NULL;
  167. list_add(&entry->tv.head, validated);
  168. }
  169. /**
  170. * amdgpu_vm_validate_pt_bos - validate the page table BOs
  171. *
  172. * @adev: amdgpu device pointer
  173. * @vm: vm providing the BOs
  174. * @validate: callback to do the validation
  175. * @param: parameter for the validation callback
  176. *
  177. * Validate the page table BOs on command submission if neccessary.
  178. */
  179. int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  180. int (*validate)(void *p, struct amdgpu_bo *bo),
  181. void *param)
  182. {
  183. struct ttm_bo_global *glob = adev->mman.bdev.glob;
  184. int r;
  185. spin_lock(&vm->status_lock);
  186. while (!list_empty(&vm->evicted)) {
  187. struct amdgpu_vm_bo_base *bo_base;
  188. struct amdgpu_bo *bo;
  189. bo_base = list_first_entry(&vm->evicted,
  190. struct amdgpu_vm_bo_base,
  191. vm_status);
  192. spin_unlock(&vm->status_lock);
  193. bo = bo_base->bo;
  194. BUG_ON(!bo);
  195. if (bo->parent) {
  196. r = validate(param, bo);
  197. if (r)
  198. return r;
  199. spin_lock(&glob->lru_lock);
  200. ttm_bo_move_to_lru_tail(&bo->tbo);
  201. if (bo->shadow)
  202. ttm_bo_move_to_lru_tail(&bo->shadow->tbo);
  203. spin_unlock(&glob->lru_lock);
  204. }
  205. if (bo->tbo.type == ttm_bo_type_kernel &&
  206. vm->use_cpu_for_update) {
  207. r = amdgpu_bo_kmap(bo, NULL);
  208. if (r)
  209. return r;
  210. }
  211. spin_lock(&vm->status_lock);
  212. if (bo->tbo.type != ttm_bo_type_kernel)
  213. list_move(&bo_base->vm_status, &vm->moved);
  214. else
  215. list_move(&bo_base->vm_status, &vm->relocated);
  216. }
  217. spin_unlock(&vm->status_lock);
  218. return 0;
  219. }
  220. /**
  221. * amdgpu_vm_ready - check VM is ready for updates
  222. *
  223. * @vm: VM to check
  224. *
  225. * Check if all VM PDs/PTs are ready for updates
  226. */
  227. bool amdgpu_vm_ready(struct amdgpu_vm *vm)
  228. {
  229. bool ready;
  230. spin_lock(&vm->status_lock);
  231. ready = list_empty(&vm->evicted);
  232. spin_unlock(&vm->status_lock);
  233. return ready;
  234. }
  235. /**
  236. * amdgpu_vm_clear_bo - initially clear the PDs/PTs
  237. *
  238. * @adev: amdgpu_device pointer
  239. * @bo: BO to clear
  240. * @level: level this BO is at
  241. *
  242. * Root PD needs to be reserved when calling this.
  243. */
  244. static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
  245. struct amdgpu_vm *vm, struct amdgpu_bo *bo,
  246. unsigned level, bool pte_support_ats)
  247. {
  248. struct ttm_operation_ctx ctx = { true, false };
  249. struct dma_fence *fence = NULL;
  250. unsigned entries, ats_entries;
  251. struct amdgpu_ring *ring;
  252. struct amdgpu_job *job;
  253. uint64_t addr;
  254. int r;
  255. addr = amdgpu_bo_gpu_offset(bo);
  256. entries = amdgpu_bo_size(bo) / 8;
  257. if (pte_support_ats) {
  258. if (level == adev->vm_manager.root_level) {
  259. ats_entries = amdgpu_vm_level_shift(adev, level);
  260. ats_entries += AMDGPU_GPU_PAGE_SHIFT;
  261. ats_entries = AMDGPU_VA_HOLE_START >> ats_entries;
  262. ats_entries = min(ats_entries, entries);
  263. entries -= ats_entries;
  264. } else {
  265. ats_entries = entries;
  266. entries = 0;
  267. }
  268. } else {
  269. ats_entries = 0;
  270. }
  271. ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
  272. r = reservation_object_reserve_shared(bo->tbo.resv);
  273. if (r)
  274. return r;
  275. r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  276. if (r)
  277. goto error;
  278. r = amdgpu_job_alloc_with_ib(adev, 64, &job);
  279. if (r)
  280. goto error;
  281. if (ats_entries) {
  282. uint64_t ats_value;
  283. ats_value = AMDGPU_PTE_DEFAULT_ATC;
  284. if (level != AMDGPU_VM_PTB)
  285. ats_value |= AMDGPU_PDE_PTE;
  286. amdgpu_vm_set_pte_pde(adev, &job->ibs[0], addr, 0,
  287. ats_entries, 0, ats_value);
  288. addr += ats_entries * 8;
  289. }
  290. if (entries)
  291. amdgpu_vm_set_pte_pde(adev, &job->ibs[0], addr, 0,
  292. entries, 0, 0);
  293. amdgpu_ring_pad_ib(ring, &job->ibs[0]);
  294. WARN_ON(job->ibs[0].length_dw > 64);
  295. r = amdgpu_sync_resv(adev, &job->sync, bo->tbo.resv,
  296. AMDGPU_FENCE_OWNER_UNDEFINED, false);
  297. if (r)
  298. goto error_free;
  299. r = amdgpu_job_submit(job, ring, &vm->entity,
  300. AMDGPU_FENCE_OWNER_UNDEFINED, &fence);
  301. if (r)
  302. goto error_free;
  303. amdgpu_bo_fence(bo, fence, true);
  304. dma_fence_put(fence);
  305. if (bo->shadow)
  306. return amdgpu_vm_clear_bo(adev, vm, bo->shadow,
  307. level, pte_support_ats);
  308. return 0;
  309. error_free:
  310. amdgpu_job_free(job);
  311. error:
  312. return r;
  313. }
  314. /**
  315. * amdgpu_vm_alloc_levels - allocate the PD/PT levels
  316. *
  317. * @adev: amdgpu_device pointer
  318. * @vm: requested vm
  319. * @saddr: start of the address range
  320. * @eaddr: end of the address range
  321. *
  322. * Make sure the page directories and page tables are allocated
  323. */
  324. static int amdgpu_vm_alloc_levels(struct amdgpu_device *adev,
  325. struct amdgpu_vm *vm,
  326. struct amdgpu_vm_pt *parent,
  327. uint64_t saddr, uint64_t eaddr,
  328. unsigned level, bool ats)
  329. {
  330. unsigned shift = amdgpu_vm_level_shift(adev, level);
  331. unsigned pt_idx, from, to;
  332. u64 flags;
  333. int r;
  334. if (!parent->entries) {
  335. unsigned num_entries = amdgpu_vm_num_entries(adev, level);
  336. parent->entries = kvmalloc_array(num_entries,
  337. sizeof(struct amdgpu_vm_pt),
  338. GFP_KERNEL | __GFP_ZERO);
  339. if (!parent->entries)
  340. return -ENOMEM;
  341. memset(parent->entries, 0 , sizeof(struct amdgpu_vm_pt));
  342. }
  343. from = saddr >> shift;
  344. to = eaddr >> shift;
  345. if (from >= amdgpu_vm_num_entries(adev, level) ||
  346. to >= amdgpu_vm_num_entries(adev, level))
  347. return -EINVAL;
  348. ++level;
  349. saddr = saddr & ((1 << shift) - 1);
  350. eaddr = eaddr & ((1 << shift) - 1);
  351. flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
  352. if (vm->use_cpu_for_update)
  353. flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
  354. else
  355. flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
  356. AMDGPU_GEM_CREATE_SHADOW);
  357. /* walk over the address space and allocate the page tables */
  358. for (pt_idx = from; pt_idx <= to; ++pt_idx) {
  359. struct reservation_object *resv = vm->root.base.bo->tbo.resv;
  360. struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
  361. struct amdgpu_bo *pt;
  362. if (!entry->base.bo) {
  363. r = amdgpu_bo_create(adev,
  364. amdgpu_vm_bo_size(adev, level),
  365. AMDGPU_GPU_PAGE_SIZE, true,
  366. AMDGPU_GEM_DOMAIN_VRAM, flags,
  367. NULL, resv, &pt);
  368. if (r)
  369. return r;
  370. r = amdgpu_vm_clear_bo(adev, vm, pt, level, ats);
  371. if (r) {
  372. amdgpu_bo_unref(&pt->shadow);
  373. amdgpu_bo_unref(&pt);
  374. return r;
  375. }
  376. if (vm->use_cpu_for_update) {
  377. r = amdgpu_bo_kmap(pt, NULL);
  378. if (r) {
  379. amdgpu_bo_unref(&pt->shadow);
  380. amdgpu_bo_unref(&pt);
  381. return r;
  382. }
  383. }
  384. /* Keep a reference to the root directory to avoid
  385. * freeing them up in the wrong order.
  386. */
  387. pt->parent = amdgpu_bo_ref(parent->base.bo);
  388. entry->base.vm = vm;
  389. entry->base.bo = pt;
  390. list_add_tail(&entry->base.bo_list, &pt->va);
  391. spin_lock(&vm->status_lock);
  392. list_add(&entry->base.vm_status, &vm->relocated);
  393. spin_unlock(&vm->status_lock);
  394. }
  395. if (level < AMDGPU_VM_PTB) {
  396. uint64_t sub_saddr = (pt_idx == from) ? saddr : 0;
  397. uint64_t sub_eaddr = (pt_idx == to) ? eaddr :
  398. ((1 << shift) - 1);
  399. r = amdgpu_vm_alloc_levels(adev, vm, entry, sub_saddr,
  400. sub_eaddr, level, ats);
  401. if (r)
  402. return r;
  403. }
  404. }
  405. return 0;
  406. }
  407. /**
  408. * amdgpu_vm_alloc_pts - Allocate page tables.
  409. *
  410. * @adev: amdgpu_device pointer
  411. * @vm: VM to allocate page tables for
  412. * @saddr: Start address which needs to be allocated
  413. * @size: Size from start address we need.
  414. *
  415. * Make sure the page tables are allocated.
  416. */
  417. int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
  418. struct amdgpu_vm *vm,
  419. uint64_t saddr, uint64_t size)
  420. {
  421. uint64_t eaddr;
  422. bool ats = false;
  423. /* validate the parameters */
  424. if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK)
  425. return -EINVAL;
  426. eaddr = saddr + size - 1;
  427. if (vm->pte_support_ats)
  428. ats = saddr < AMDGPU_VA_HOLE_START;
  429. saddr /= AMDGPU_GPU_PAGE_SIZE;
  430. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  431. if (eaddr >= adev->vm_manager.max_pfn) {
  432. dev_err(adev->dev, "va above limit (0x%08llX >= 0x%08llX)\n",
  433. eaddr, adev->vm_manager.max_pfn);
  434. return -EINVAL;
  435. }
  436. return amdgpu_vm_alloc_levels(adev, vm, &vm->root, saddr, eaddr,
  437. adev->vm_manager.root_level, ats);
  438. }
  439. /**
  440. * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug
  441. *
  442. * @adev: amdgpu_device pointer
  443. */
  444. void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev)
  445. {
  446. const struct amdgpu_ip_block *ip_block;
  447. bool has_compute_vm_bug;
  448. struct amdgpu_ring *ring;
  449. int i;
  450. has_compute_vm_bug = false;
  451. ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
  452. if (ip_block) {
  453. /* Compute has a VM bug for GFX version < 7.
  454. Compute has a VM bug for GFX 8 MEC firmware version < 673.*/
  455. if (ip_block->version->major <= 7)
  456. has_compute_vm_bug = true;
  457. else if (ip_block->version->major == 8)
  458. if (adev->gfx.mec_fw_version < 673)
  459. has_compute_vm_bug = true;
  460. }
  461. for (i = 0; i < adev->num_rings; i++) {
  462. ring = adev->rings[i];
  463. if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE)
  464. /* only compute rings */
  465. ring->has_compute_vm_bug = has_compute_vm_bug;
  466. else
  467. ring->has_compute_vm_bug = false;
  468. }
  469. }
  470. bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
  471. struct amdgpu_job *job)
  472. {
  473. struct amdgpu_device *adev = ring->adev;
  474. unsigned vmhub = ring->funcs->vmhub;
  475. struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
  476. struct amdgpu_vmid *id;
  477. bool gds_switch_needed;
  478. bool vm_flush_needed = job->vm_needs_flush || ring->has_compute_vm_bug;
  479. if (job->vmid == 0)
  480. return false;
  481. id = &id_mgr->ids[job->vmid];
  482. gds_switch_needed = ring->funcs->emit_gds_switch && (
  483. id->gds_base != job->gds_base ||
  484. id->gds_size != job->gds_size ||
  485. id->gws_base != job->gws_base ||
  486. id->gws_size != job->gws_size ||
  487. id->oa_base != job->oa_base ||
  488. id->oa_size != job->oa_size);
  489. if (amdgpu_vmid_had_gpu_reset(adev, id))
  490. return true;
  491. return vm_flush_needed || gds_switch_needed;
  492. }
  493. static bool amdgpu_vm_is_large_bar(struct amdgpu_device *adev)
  494. {
  495. return (adev->gmc.real_vram_size == adev->gmc.visible_vram_size);
  496. }
  497. /**
  498. * amdgpu_vm_flush - hardware flush the vm
  499. *
  500. * @ring: ring to use for flush
  501. * @vmid: vmid number to use
  502. * @pd_addr: address of the page directory
  503. *
  504. * Emit a VM flush when it is necessary.
  505. */
  506. int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync)
  507. {
  508. struct amdgpu_device *adev = ring->adev;
  509. unsigned vmhub = ring->funcs->vmhub;
  510. struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
  511. struct amdgpu_vmid *id = &id_mgr->ids[job->vmid];
  512. bool gds_switch_needed = ring->funcs->emit_gds_switch && (
  513. id->gds_base != job->gds_base ||
  514. id->gds_size != job->gds_size ||
  515. id->gws_base != job->gws_base ||
  516. id->gws_size != job->gws_size ||
  517. id->oa_base != job->oa_base ||
  518. id->oa_size != job->oa_size);
  519. bool vm_flush_needed = job->vm_needs_flush;
  520. unsigned patch_offset = 0;
  521. int r;
  522. if (amdgpu_vmid_had_gpu_reset(adev, id)) {
  523. gds_switch_needed = true;
  524. vm_flush_needed = true;
  525. }
  526. if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync)
  527. return 0;
  528. if (ring->funcs->init_cond_exec)
  529. patch_offset = amdgpu_ring_init_cond_exec(ring);
  530. if (need_pipe_sync)
  531. amdgpu_ring_emit_pipeline_sync(ring);
  532. if (ring->funcs->emit_vm_flush && vm_flush_needed) {
  533. struct dma_fence *fence;
  534. trace_amdgpu_vm_flush(ring, job->vmid, job->vm_pd_addr);
  535. amdgpu_ring_emit_vm_flush(ring, job->vmid, job->vm_pd_addr);
  536. if (adev->gmc.gmc_funcs->emit_pasid_mapping &&
  537. ring->funcs->emit_wreg)
  538. amdgpu_gmc_emit_pasid_mapping(ring, job->vmid,
  539. job->pasid);
  540. r = amdgpu_fence_emit(ring, &fence);
  541. if (r)
  542. return r;
  543. mutex_lock(&id_mgr->lock);
  544. dma_fence_put(id->last_flush);
  545. id->last_flush = fence;
  546. id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
  547. mutex_unlock(&id_mgr->lock);
  548. }
  549. if (ring->funcs->emit_gds_switch && gds_switch_needed) {
  550. id->gds_base = job->gds_base;
  551. id->gds_size = job->gds_size;
  552. id->gws_base = job->gws_base;
  553. id->gws_size = job->gws_size;
  554. id->oa_base = job->oa_base;
  555. id->oa_size = job->oa_size;
  556. amdgpu_ring_emit_gds_switch(ring, job->vmid, job->gds_base,
  557. job->gds_size, job->gws_base,
  558. job->gws_size, job->oa_base,
  559. job->oa_size);
  560. }
  561. if (ring->funcs->patch_cond_exec)
  562. amdgpu_ring_patch_cond_exec(ring, patch_offset);
  563. /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
  564. if (ring->funcs->emit_switch_buffer) {
  565. amdgpu_ring_emit_switch_buffer(ring);
  566. amdgpu_ring_emit_switch_buffer(ring);
  567. }
  568. return 0;
  569. }
  570. /**
  571. * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
  572. *
  573. * @vm: requested vm
  574. * @bo: requested buffer object
  575. *
  576. * Find @bo inside the requested vm.
  577. * Search inside the @bos vm list for the requested vm
  578. * Returns the found bo_va or NULL if none is found
  579. *
  580. * Object has to be reserved!
  581. */
  582. struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
  583. struct amdgpu_bo *bo)
  584. {
  585. struct amdgpu_bo_va *bo_va;
  586. list_for_each_entry(bo_va, &bo->va, base.bo_list) {
  587. if (bo_va->base.vm == vm) {
  588. return bo_va;
  589. }
  590. }
  591. return NULL;
  592. }
  593. /**
  594. * amdgpu_vm_do_set_ptes - helper to call the right asic function
  595. *
  596. * @params: see amdgpu_pte_update_params definition
  597. * @bo: PD/PT to update
  598. * @pe: addr of the page entry
  599. * @addr: dst addr to write into pe
  600. * @count: number of page entries to update
  601. * @incr: increase next addr by incr bytes
  602. * @flags: hw access flags
  603. *
  604. * Traces the parameters and calls the right asic functions
  605. * to setup the page table using the DMA.
  606. */
  607. static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
  608. struct amdgpu_bo *bo,
  609. uint64_t pe, uint64_t addr,
  610. unsigned count, uint32_t incr,
  611. uint64_t flags)
  612. {
  613. pe += amdgpu_bo_gpu_offset(bo);
  614. trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
  615. if (count < 3) {
  616. amdgpu_vm_write_pte(params->adev, params->ib, pe,
  617. addr | flags, count, incr);
  618. } else {
  619. amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
  620. count, incr, flags);
  621. }
  622. }
  623. /**
  624. * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
  625. *
  626. * @params: see amdgpu_pte_update_params definition
  627. * @bo: PD/PT to update
  628. * @pe: addr of the page entry
  629. * @addr: dst addr to write into pe
  630. * @count: number of page entries to update
  631. * @incr: increase next addr by incr bytes
  632. * @flags: hw access flags
  633. *
  634. * Traces the parameters and calls the DMA function to copy the PTEs.
  635. */
  636. static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
  637. struct amdgpu_bo *bo,
  638. uint64_t pe, uint64_t addr,
  639. unsigned count, uint32_t incr,
  640. uint64_t flags)
  641. {
  642. uint64_t src = (params->src + (addr >> 12) * 8);
  643. pe += amdgpu_bo_gpu_offset(bo);
  644. trace_amdgpu_vm_copy_ptes(pe, src, count);
  645. amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count);
  646. }
  647. /**
  648. * amdgpu_vm_map_gart - Resolve gart mapping of addr
  649. *
  650. * @pages_addr: optional DMA address to use for lookup
  651. * @addr: the unmapped addr
  652. *
  653. * Look up the physical address of the page that the pte resolves
  654. * to and return the pointer for the page table entry.
  655. */
  656. static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
  657. {
  658. uint64_t result;
  659. /* page table offset */
  660. result = pages_addr[addr >> PAGE_SHIFT];
  661. /* in case cpu page size != gpu page size*/
  662. result |= addr & (~PAGE_MASK);
  663. result &= 0xFFFFFFFFFFFFF000ULL;
  664. return result;
  665. }
  666. /**
  667. * amdgpu_vm_cpu_set_ptes - helper to update page tables via CPU
  668. *
  669. * @params: see amdgpu_pte_update_params definition
  670. * @bo: PD/PT to update
  671. * @pe: kmap addr of the page entry
  672. * @addr: dst addr to write into pe
  673. * @count: number of page entries to update
  674. * @incr: increase next addr by incr bytes
  675. * @flags: hw access flags
  676. *
  677. * Write count number of PT/PD entries directly.
  678. */
  679. static void amdgpu_vm_cpu_set_ptes(struct amdgpu_pte_update_params *params,
  680. struct amdgpu_bo *bo,
  681. uint64_t pe, uint64_t addr,
  682. unsigned count, uint32_t incr,
  683. uint64_t flags)
  684. {
  685. unsigned int i;
  686. uint64_t value;
  687. pe += (unsigned long)amdgpu_bo_kptr(bo);
  688. trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
  689. for (i = 0; i < count; i++) {
  690. value = params->pages_addr ?
  691. amdgpu_vm_map_gart(params->pages_addr, addr) :
  692. addr;
  693. amdgpu_gmc_set_pte_pde(params->adev, (void *)(uintptr_t)pe,
  694. i, value, flags);
  695. addr += incr;
  696. }
  697. }
  698. static int amdgpu_vm_wait_pd(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  699. void *owner)
  700. {
  701. struct amdgpu_sync sync;
  702. int r;
  703. amdgpu_sync_create(&sync);
  704. amdgpu_sync_resv(adev, &sync, vm->root.base.bo->tbo.resv, owner, false);
  705. r = amdgpu_sync_wait(&sync, true);
  706. amdgpu_sync_free(&sync);
  707. return r;
  708. }
  709. /*
  710. * amdgpu_vm_update_pde - update a single level in the hierarchy
  711. *
  712. * @param: parameters for the update
  713. * @vm: requested vm
  714. * @parent: parent directory
  715. * @entry: entry to update
  716. *
  717. * Makes sure the requested entry in parent is up to date.
  718. */
  719. static void amdgpu_vm_update_pde(struct amdgpu_pte_update_params *params,
  720. struct amdgpu_vm *vm,
  721. struct amdgpu_vm_pt *parent,
  722. struct amdgpu_vm_pt *entry)
  723. {
  724. struct amdgpu_bo *bo = parent->base.bo, *pbo;
  725. uint64_t pde, pt, flags;
  726. unsigned level;
  727. /* Don't update huge pages here */
  728. if (entry->huge)
  729. return;
  730. for (level = 0, pbo = bo->parent; pbo; ++level)
  731. pbo = pbo->parent;
  732. level += params->adev->vm_manager.root_level;
  733. pt = amdgpu_bo_gpu_offset(entry->base.bo);
  734. flags = AMDGPU_PTE_VALID;
  735. amdgpu_gmc_get_vm_pde(params->adev, level, &pt, &flags);
  736. pde = (entry - parent->entries) * 8;
  737. if (bo->shadow)
  738. params->func(params, bo->shadow, pde, pt, 1, 0, flags);
  739. params->func(params, bo, pde, pt, 1, 0, flags);
  740. }
  741. /*
  742. * amdgpu_vm_invalidate_level - mark all PD levels as invalid
  743. *
  744. * @parent: parent PD
  745. *
  746. * Mark all PD level as invalid after an error.
  747. */
  748. static void amdgpu_vm_invalidate_level(struct amdgpu_device *adev,
  749. struct amdgpu_vm *vm,
  750. struct amdgpu_vm_pt *parent,
  751. unsigned level)
  752. {
  753. unsigned pt_idx, num_entries;
  754. /*
  755. * Recurse into the subdirectories. This recursion is harmless because
  756. * we only have a maximum of 5 layers.
  757. */
  758. num_entries = amdgpu_vm_num_entries(adev, level);
  759. for (pt_idx = 0; pt_idx < num_entries; ++pt_idx) {
  760. struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
  761. if (!entry->base.bo)
  762. continue;
  763. spin_lock(&vm->status_lock);
  764. if (list_empty(&entry->base.vm_status))
  765. list_add(&entry->base.vm_status, &vm->relocated);
  766. spin_unlock(&vm->status_lock);
  767. amdgpu_vm_invalidate_level(adev, vm, entry, level + 1);
  768. }
  769. }
  770. /*
  771. * amdgpu_vm_update_directories - make sure that all directories are valid
  772. *
  773. * @adev: amdgpu_device pointer
  774. * @vm: requested vm
  775. *
  776. * Makes sure all directories are up to date.
  777. * Returns 0 for success, error for failure.
  778. */
  779. int amdgpu_vm_update_directories(struct amdgpu_device *adev,
  780. struct amdgpu_vm *vm)
  781. {
  782. struct amdgpu_pte_update_params params;
  783. struct amdgpu_job *job;
  784. unsigned ndw = 0;
  785. int r = 0;
  786. if (list_empty(&vm->relocated))
  787. return 0;
  788. restart:
  789. memset(&params, 0, sizeof(params));
  790. params.adev = adev;
  791. if (vm->use_cpu_for_update) {
  792. r = amdgpu_vm_wait_pd(adev, vm, AMDGPU_FENCE_OWNER_VM);
  793. if (unlikely(r))
  794. return r;
  795. params.func = amdgpu_vm_cpu_set_ptes;
  796. } else {
  797. ndw = 512 * 8;
  798. r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
  799. if (r)
  800. return r;
  801. params.ib = &job->ibs[0];
  802. params.func = amdgpu_vm_do_set_ptes;
  803. }
  804. spin_lock(&vm->status_lock);
  805. while (!list_empty(&vm->relocated)) {
  806. struct amdgpu_vm_bo_base *bo_base, *parent;
  807. struct amdgpu_vm_pt *pt, *entry;
  808. struct amdgpu_bo *bo;
  809. bo_base = list_first_entry(&vm->relocated,
  810. struct amdgpu_vm_bo_base,
  811. vm_status);
  812. list_del_init(&bo_base->vm_status);
  813. spin_unlock(&vm->status_lock);
  814. bo = bo_base->bo->parent;
  815. if (!bo) {
  816. spin_lock(&vm->status_lock);
  817. continue;
  818. }
  819. parent = list_first_entry(&bo->va, struct amdgpu_vm_bo_base,
  820. bo_list);
  821. pt = container_of(parent, struct amdgpu_vm_pt, base);
  822. entry = container_of(bo_base, struct amdgpu_vm_pt, base);
  823. amdgpu_vm_update_pde(&params, vm, pt, entry);
  824. spin_lock(&vm->status_lock);
  825. if (!vm->use_cpu_for_update &&
  826. (ndw - params.ib->length_dw) < 32)
  827. break;
  828. }
  829. spin_unlock(&vm->status_lock);
  830. if (vm->use_cpu_for_update) {
  831. /* Flush HDP */
  832. mb();
  833. amdgpu_asic_flush_hdp(adev, NULL);
  834. } else if (params.ib->length_dw == 0) {
  835. amdgpu_job_free(job);
  836. } else {
  837. struct amdgpu_bo *root = vm->root.base.bo;
  838. struct amdgpu_ring *ring;
  839. struct dma_fence *fence;
  840. ring = container_of(vm->entity.sched, struct amdgpu_ring,
  841. sched);
  842. amdgpu_ring_pad_ib(ring, params.ib);
  843. amdgpu_sync_resv(adev, &job->sync, root->tbo.resv,
  844. AMDGPU_FENCE_OWNER_VM, false);
  845. WARN_ON(params.ib->length_dw > ndw);
  846. r = amdgpu_job_submit(job, ring, &vm->entity,
  847. AMDGPU_FENCE_OWNER_VM, &fence);
  848. if (r)
  849. goto error;
  850. amdgpu_bo_fence(root, fence, true);
  851. dma_fence_put(vm->last_update);
  852. vm->last_update = fence;
  853. }
  854. if (!list_empty(&vm->relocated))
  855. goto restart;
  856. return 0;
  857. error:
  858. amdgpu_vm_invalidate_level(adev, vm, &vm->root,
  859. adev->vm_manager.root_level);
  860. amdgpu_job_free(job);
  861. return r;
  862. }
  863. /**
  864. * amdgpu_vm_find_entry - find the entry for an address
  865. *
  866. * @p: see amdgpu_pte_update_params definition
  867. * @addr: virtual address in question
  868. * @entry: resulting entry or NULL
  869. * @parent: parent entry
  870. *
  871. * Find the vm_pt entry and it's parent for the given address.
  872. */
  873. void amdgpu_vm_get_entry(struct amdgpu_pte_update_params *p, uint64_t addr,
  874. struct amdgpu_vm_pt **entry,
  875. struct amdgpu_vm_pt **parent)
  876. {
  877. unsigned level = p->adev->vm_manager.root_level;
  878. *parent = NULL;
  879. *entry = &p->vm->root;
  880. while ((*entry)->entries) {
  881. unsigned shift = amdgpu_vm_level_shift(p->adev, level++);
  882. *parent = *entry;
  883. *entry = &(*entry)->entries[addr >> shift];
  884. addr &= (1ULL << shift) - 1;
  885. }
  886. if (level != AMDGPU_VM_PTB)
  887. *entry = NULL;
  888. }
  889. /**
  890. * amdgpu_vm_handle_huge_pages - handle updating the PD with huge pages
  891. *
  892. * @p: see amdgpu_pte_update_params definition
  893. * @entry: vm_pt entry to check
  894. * @parent: parent entry
  895. * @nptes: number of PTEs updated with this operation
  896. * @dst: destination address where the PTEs should point to
  897. * @flags: access flags fro the PTEs
  898. *
  899. * Check if we can update the PD with a huge page.
  900. */
  901. static void amdgpu_vm_handle_huge_pages(struct amdgpu_pte_update_params *p,
  902. struct amdgpu_vm_pt *entry,
  903. struct amdgpu_vm_pt *parent,
  904. unsigned nptes, uint64_t dst,
  905. uint64_t flags)
  906. {
  907. uint64_t pde;
  908. /* In the case of a mixed PT the PDE must point to it*/
  909. if (p->adev->asic_type >= CHIP_VEGA10 && !p->src &&
  910. nptes == AMDGPU_VM_PTE_COUNT(p->adev)) {
  911. /* Set the huge page flag to stop scanning at this PDE */
  912. flags |= AMDGPU_PDE_PTE;
  913. }
  914. if (!(flags & AMDGPU_PDE_PTE)) {
  915. if (entry->huge) {
  916. /* Add the entry to the relocated list to update it. */
  917. entry->huge = false;
  918. spin_lock(&p->vm->status_lock);
  919. list_move(&entry->base.vm_status, &p->vm->relocated);
  920. spin_unlock(&p->vm->status_lock);
  921. }
  922. return;
  923. }
  924. entry->huge = true;
  925. amdgpu_gmc_get_vm_pde(p->adev, AMDGPU_VM_PDB0, &dst, &flags);
  926. pde = (entry - parent->entries) * 8;
  927. if (parent->base.bo->shadow)
  928. p->func(p, parent->base.bo->shadow, pde, dst, 1, 0, flags);
  929. p->func(p, parent->base.bo, pde, dst, 1, 0, flags);
  930. }
  931. /**
  932. * amdgpu_vm_update_ptes - make sure that page tables are valid
  933. *
  934. * @params: see amdgpu_pte_update_params definition
  935. * @vm: requested vm
  936. * @start: start of GPU address range
  937. * @end: end of GPU address range
  938. * @dst: destination address to map to, the next dst inside the function
  939. * @flags: mapping flags
  940. *
  941. * Update the page tables in the range @start - @end.
  942. * Returns 0 for success, -EINVAL for failure.
  943. */
  944. static int amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
  945. uint64_t start, uint64_t end,
  946. uint64_t dst, uint64_t flags)
  947. {
  948. struct amdgpu_device *adev = params->adev;
  949. const uint64_t mask = AMDGPU_VM_PTE_COUNT(adev) - 1;
  950. uint64_t addr, pe_start;
  951. struct amdgpu_bo *pt;
  952. unsigned nptes;
  953. /* walk over the address space and update the page tables */
  954. for (addr = start; addr < end; addr += nptes,
  955. dst += nptes * AMDGPU_GPU_PAGE_SIZE) {
  956. struct amdgpu_vm_pt *entry, *parent;
  957. amdgpu_vm_get_entry(params, addr, &entry, &parent);
  958. if (!entry)
  959. return -ENOENT;
  960. if ((addr & ~mask) == (end & ~mask))
  961. nptes = end - addr;
  962. else
  963. nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask);
  964. amdgpu_vm_handle_huge_pages(params, entry, parent,
  965. nptes, dst, flags);
  966. /* We don't need to update PTEs for huge pages */
  967. if (entry->huge)
  968. continue;
  969. pt = entry->base.bo;
  970. pe_start = (addr & mask) * 8;
  971. if (pt->shadow)
  972. params->func(params, pt->shadow, pe_start, dst, nptes,
  973. AMDGPU_GPU_PAGE_SIZE, flags);
  974. params->func(params, pt, pe_start, dst, nptes,
  975. AMDGPU_GPU_PAGE_SIZE, flags);
  976. }
  977. return 0;
  978. }
  979. /*
  980. * amdgpu_vm_frag_ptes - add fragment information to PTEs
  981. *
  982. * @params: see amdgpu_pte_update_params definition
  983. * @vm: requested vm
  984. * @start: first PTE to handle
  985. * @end: last PTE to handle
  986. * @dst: addr those PTEs should point to
  987. * @flags: hw mapping flags
  988. * Returns 0 for success, -EINVAL for failure.
  989. */
  990. static int amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
  991. uint64_t start, uint64_t end,
  992. uint64_t dst, uint64_t flags)
  993. {
  994. /**
  995. * The MC L1 TLB supports variable sized pages, based on a fragment
  996. * field in the PTE. When this field is set to a non-zero value, page
  997. * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
  998. * flags are considered valid for all PTEs within the fragment range
  999. * and corresponding mappings are assumed to be physically contiguous.
  1000. *
  1001. * The L1 TLB can store a single PTE for the whole fragment,
  1002. * significantly increasing the space available for translation
  1003. * caching. This leads to large improvements in throughput when the
  1004. * TLB is under pressure.
  1005. *
  1006. * The L2 TLB distributes small and large fragments into two
  1007. * asymmetric partitions. The large fragment cache is significantly
  1008. * larger. Thus, we try to use large fragments wherever possible.
  1009. * Userspace can support this by aligning virtual base address and
  1010. * allocation size to the fragment size.
  1011. */
  1012. unsigned max_frag = params->adev->vm_manager.fragment_size;
  1013. int r;
  1014. /* system pages are non continuously */
  1015. if (params->src || !(flags & AMDGPU_PTE_VALID))
  1016. return amdgpu_vm_update_ptes(params, start, end, dst, flags);
  1017. while (start != end) {
  1018. uint64_t frag_flags, frag_end;
  1019. unsigned frag;
  1020. /* This intentionally wraps around if no bit is set */
  1021. frag = min((unsigned)ffs(start) - 1,
  1022. (unsigned)fls64(end - start) - 1);
  1023. if (frag >= max_frag) {
  1024. frag_flags = AMDGPU_PTE_FRAG(max_frag);
  1025. frag_end = end & ~((1ULL << max_frag) - 1);
  1026. } else {
  1027. frag_flags = AMDGPU_PTE_FRAG(frag);
  1028. frag_end = start + (1 << frag);
  1029. }
  1030. r = amdgpu_vm_update_ptes(params, start, frag_end, dst,
  1031. flags | frag_flags);
  1032. if (r)
  1033. return r;
  1034. dst += (frag_end - start) * AMDGPU_GPU_PAGE_SIZE;
  1035. start = frag_end;
  1036. }
  1037. return 0;
  1038. }
  1039. /**
  1040. * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
  1041. *
  1042. * @adev: amdgpu_device pointer
  1043. * @exclusive: fence we need to sync to
  1044. * @pages_addr: DMA addresses to use for mapping
  1045. * @vm: requested vm
  1046. * @start: start of mapped range
  1047. * @last: last mapped entry
  1048. * @flags: flags for the entries
  1049. * @addr: addr to set the area to
  1050. * @fence: optional resulting fence
  1051. *
  1052. * Fill in the page table entries between @start and @last.
  1053. * Returns 0 for success, -EINVAL for failure.
  1054. */
  1055. static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
  1056. struct dma_fence *exclusive,
  1057. dma_addr_t *pages_addr,
  1058. struct amdgpu_vm *vm,
  1059. uint64_t start, uint64_t last,
  1060. uint64_t flags, uint64_t addr,
  1061. struct dma_fence **fence)
  1062. {
  1063. struct amdgpu_ring *ring;
  1064. void *owner = AMDGPU_FENCE_OWNER_VM;
  1065. unsigned nptes, ncmds, ndw;
  1066. struct amdgpu_job *job;
  1067. struct amdgpu_pte_update_params params;
  1068. struct dma_fence *f = NULL;
  1069. int r;
  1070. memset(&params, 0, sizeof(params));
  1071. params.adev = adev;
  1072. params.vm = vm;
  1073. /* sync to everything on unmapping */
  1074. if (!(flags & AMDGPU_PTE_VALID))
  1075. owner = AMDGPU_FENCE_OWNER_UNDEFINED;
  1076. if (vm->use_cpu_for_update) {
  1077. /* params.src is used as flag to indicate system Memory */
  1078. if (pages_addr)
  1079. params.src = ~0;
  1080. /* Wait for PT BOs to be free. PTs share the same resv. object
  1081. * as the root PD BO
  1082. */
  1083. r = amdgpu_vm_wait_pd(adev, vm, owner);
  1084. if (unlikely(r))
  1085. return r;
  1086. params.func = amdgpu_vm_cpu_set_ptes;
  1087. params.pages_addr = pages_addr;
  1088. return amdgpu_vm_frag_ptes(&params, start, last + 1,
  1089. addr, flags);
  1090. }
  1091. ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
  1092. nptes = last - start + 1;
  1093. /*
  1094. * reserve space for two commands every (1 << BLOCK_SIZE)
  1095. * entries or 2k dwords (whatever is smaller)
  1096. *
  1097. * The second command is for the shadow pagetables.
  1098. */
  1099. if (vm->root.base.bo->shadow)
  1100. ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1) * 2;
  1101. else
  1102. ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1);
  1103. /* padding, etc. */
  1104. ndw = 64;
  1105. if (pages_addr) {
  1106. /* copy commands needed */
  1107. ndw += ncmds * adev->vm_manager.vm_pte_funcs->copy_pte_num_dw;
  1108. /* and also PTEs */
  1109. ndw += nptes * 2;
  1110. params.func = amdgpu_vm_do_copy_ptes;
  1111. } else {
  1112. /* set page commands needed */
  1113. ndw += ncmds * 10;
  1114. /* extra commands for begin/end fragments */
  1115. ndw += 2 * 10 * adev->vm_manager.fragment_size;
  1116. params.func = amdgpu_vm_do_set_ptes;
  1117. }
  1118. r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
  1119. if (r)
  1120. return r;
  1121. params.ib = &job->ibs[0];
  1122. if (pages_addr) {
  1123. uint64_t *pte;
  1124. unsigned i;
  1125. /* Put the PTEs at the end of the IB. */
  1126. i = ndw - nptes * 2;
  1127. pte= (uint64_t *)&(job->ibs->ptr[i]);
  1128. params.src = job->ibs->gpu_addr + i * 4;
  1129. for (i = 0; i < nptes; ++i) {
  1130. pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
  1131. AMDGPU_GPU_PAGE_SIZE);
  1132. pte[i] |= flags;
  1133. }
  1134. addr = 0;
  1135. }
  1136. r = amdgpu_sync_fence(adev, &job->sync, exclusive, false);
  1137. if (r)
  1138. goto error_free;
  1139. r = amdgpu_sync_resv(adev, &job->sync, vm->root.base.bo->tbo.resv,
  1140. owner, false);
  1141. if (r)
  1142. goto error_free;
  1143. r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv);
  1144. if (r)
  1145. goto error_free;
  1146. r = amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
  1147. if (r)
  1148. goto error_free;
  1149. amdgpu_ring_pad_ib(ring, params.ib);
  1150. WARN_ON(params.ib->length_dw > ndw);
  1151. r = amdgpu_job_submit(job, ring, &vm->entity,
  1152. AMDGPU_FENCE_OWNER_VM, &f);
  1153. if (r)
  1154. goto error_free;
  1155. amdgpu_bo_fence(vm->root.base.bo, f, true);
  1156. dma_fence_put(*fence);
  1157. *fence = f;
  1158. return 0;
  1159. error_free:
  1160. amdgpu_job_free(job);
  1161. return r;
  1162. }
  1163. /**
  1164. * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
  1165. *
  1166. * @adev: amdgpu_device pointer
  1167. * @exclusive: fence we need to sync to
  1168. * @pages_addr: DMA addresses to use for mapping
  1169. * @vm: requested vm
  1170. * @mapping: mapped range and flags to use for the update
  1171. * @flags: HW flags for the mapping
  1172. * @nodes: array of drm_mm_nodes with the MC addresses
  1173. * @fence: optional resulting fence
  1174. *
  1175. * Split the mapping into smaller chunks so that each update fits
  1176. * into a SDMA IB.
  1177. * Returns 0 for success, -EINVAL for failure.
  1178. */
  1179. static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
  1180. struct dma_fence *exclusive,
  1181. dma_addr_t *pages_addr,
  1182. struct amdgpu_vm *vm,
  1183. struct amdgpu_bo_va_mapping *mapping,
  1184. uint64_t flags,
  1185. struct drm_mm_node *nodes,
  1186. struct dma_fence **fence)
  1187. {
  1188. unsigned min_linear_pages = 1 << adev->vm_manager.fragment_size;
  1189. uint64_t pfn, start = mapping->start;
  1190. int r;
  1191. /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
  1192. * but in case of something, we filter the flags in first place
  1193. */
  1194. if (!(mapping->flags & AMDGPU_PTE_READABLE))
  1195. flags &= ~AMDGPU_PTE_READABLE;
  1196. if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
  1197. flags &= ~AMDGPU_PTE_WRITEABLE;
  1198. flags &= ~AMDGPU_PTE_EXECUTABLE;
  1199. flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
  1200. flags &= ~AMDGPU_PTE_MTYPE_MASK;
  1201. flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
  1202. if ((mapping->flags & AMDGPU_PTE_PRT) &&
  1203. (adev->asic_type >= CHIP_VEGA10)) {
  1204. flags |= AMDGPU_PTE_PRT;
  1205. flags &= ~AMDGPU_PTE_VALID;
  1206. }
  1207. trace_amdgpu_vm_bo_update(mapping);
  1208. pfn = mapping->offset >> PAGE_SHIFT;
  1209. if (nodes) {
  1210. while (pfn >= nodes->size) {
  1211. pfn -= nodes->size;
  1212. ++nodes;
  1213. }
  1214. }
  1215. do {
  1216. dma_addr_t *dma_addr = NULL;
  1217. uint64_t max_entries;
  1218. uint64_t addr, last;
  1219. if (nodes) {
  1220. addr = nodes->start << PAGE_SHIFT;
  1221. max_entries = (nodes->size - pfn) *
  1222. (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
  1223. } else {
  1224. addr = 0;
  1225. max_entries = S64_MAX;
  1226. }
  1227. if (pages_addr) {
  1228. uint64_t count;
  1229. max_entries = min(max_entries, 16ull * 1024ull);
  1230. for (count = 1; count < max_entries; ++count) {
  1231. uint64_t idx = pfn + count;
  1232. if (pages_addr[idx] !=
  1233. (pages_addr[idx - 1] + PAGE_SIZE))
  1234. break;
  1235. }
  1236. if (count < min_linear_pages) {
  1237. addr = pfn << PAGE_SHIFT;
  1238. dma_addr = pages_addr;
  1239. } else {
  1240. addr = pages_addr[pfn];
  1241. max_entries = count;
  1242. }
  1243. } else if (flags & AMDGPU_PTE_VALID) {
  1244. addr += adev->vm_manager.vram_base_offset;
  1245. addr += pfn << PAGE_SHIFT;
  1246. }
  1247. last = min((uint64_t)mapping->last, start + max_entries - 1);
  1248. r = amdgpu_vm_bo_update_mapping(adev, exclusive, dma_addr, vm,
  1249. start, last, flags, addr,
  1250. fence);
  1251. if (r)
  1252. return r;
  1253. pfn += last - start + 1;
  1254. if (nodes && nodes->size == pfn) {
  1255. pfn = 0;
  1256. ++nodes;
  1257. }
  1258. start = last + 1;
  1259. } while (unlikely(start != mapping->last + 1));
  1260. return 0;
  1261. }
  1262. /**
  1263. * amdgpu_vm_bo_update - update all BO mappings in the vm page table
  1264. *
  1265. * @adev: amdgpu_device pointer
  1266. * @bo_va: requested BO and VM object
  1267. * @clear: if true clear the entries
  1268. *
  1269. * Fill in the page table entries for @bo_va.
  1270. * Returns 0 for success, -EINVAL for failure.
  1271. */
  1272. int amdgpu_vm_bo_update(struct amdgpu_device *adev,
  1273. struct amdgpu_bo_va *bo_va,
  1274. bool clear)
  1275. {
  1276. struct amdgpu_bo *bo = bo_va->base.bo;
  1277. struct amdgpu_vm *vm = bo_va->base.vm;
  1278. struct amdgpu_bo_va_mapping *mapping;
  1279. dma_addr_t *pages_addr = NULL;
  1280. struct ttm_mem_reg *mem;
  1281. struct drm_mm_node *nodes;
  1282. struct dma_fence *exclusive, **last_update;
  1283. uint64_t flags;
  1284. int r;
  1285. if (clear || !bo_va->base.bo) {
  1286. mem = NULL;
  1287. nodes = NULL;
  1288. exclusive = NULL;
  1289. } else {
  1290. struct ttm_dma_tt *ttm;
  1291. mem = &bo_va->base.bo->tbo.mem;
  1292. nodes = mem->mm_node;
  1293. if (mem->mem_type == TTM_PL_TT) {
  1294. ttm = container_of(bo_va->base.bo->tbo.ttm,
  1295. struct ttm_dma_tt, ttm);
  1296. pages_addr = ttm->dma_address;
  1297. }
  1298. exclusive = reservation_object_get_excl(bo->tbo.resv);
  1299. }
  1300. if (bo)
  1301. flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
  1302. else
  1303. flags = 0x0;
  1304. if (clear || (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv))
  1305. last_update = &vm->last_update;
  1306. else
  1307. last_update = &bo_va->last_pt_update;
  1308. if (!clear && bo_va->base.moved) {
  1309. bo_va->base.moved = false;
  1310. list_splice_init(&bo_va->valids, &bo_va->invalids);
  1311. } else if (bo_va->cleared != clear) {
  1312. list_splice_init(&bo_va->valids, &bo_va->invalids);
  1313. }
  1314. list_for_each_entry(mapping, &bo_va->invalids, list) {
  1315. r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm,
  1316. mapping, flags, nodes,
  1317. last_update);
  1318. if (r)
  1319. return r;
  1320. }
  1321. if (vm->use_cpu_for_update) {
  1322. /* Flush HDP */
  1323. mb();
  1324. amdgpu_asic_flush_hdp(adev, NULL);
  1325. }
  1326. spin_lock(&vm->status_lock);
  1327. list_del_init(&bo_va->base.vm_status);
  1328. spin_unlock(&vm->status_lock);
  1329. list_splice_init(&bo_va->invalids, &bo_va->valids);
  1330. bo_va->cleared = clear;
  1331. if (trace_amdgpu_vm_bo_mapping_enabled()) {
  1332. list_for_each_entry(mapping, &bo_va->valids, list)
  1333. trace_amdgpu_vm_bo_mapping(mapping);
  1334. }
  1335. return 0;
  1336. }
  1337. /**
  1338. * amdgpu_vm_update_prt_state - update the global PRT state
  1339. */
  1340. static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
  1341. {
  1342. unsigned long flags;
  1343. bool enable;
  1344. spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
  1345. enable = !!atomic_read(&adev->vm_manager.num_prt_users);
  1346. adev->gmc.gmc_funcs->set_prt(adev, enable);
  1347. spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
  1348. }
  1349. /**
  1350. * amdgpu_vm_prt_get - add a PRT user
  1351. */
  1352. static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
  1353. {
  1354. if (!adev->gmc.gmc_funcs->set_prt)
  1355. return;
  1356. if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
  1357. amdgpu_vm_update_prt_state(adev);
  1358. }
  1359. /**
  1360. * amdgpu_vm_prt_put - drop a PRT user
  1361. */
  1362. static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
  1363. {
  1364. if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
  1365. amdgpu_vm_update_prt_state(adev);
  1366. }
  1367. /**
  1368. * amdgpu_vm_prt_cb - callback for updating the PRT status
  1369. */
  1370. static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
  1371. {
  1372. struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
  1373. amdgpu_vm_prt_put(cb->adev);
  1374. kfree(cb);
  1375. }
  1376. /**
  1377. * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
  1378. */
  1379. static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
  1380. struct dma_fence *fence)
  1381. {
  1382. struct amdgpu_prt_cb *cb;
  1383. if (!adev->gmc.gmc_funcs->set_prt)
  1384. return;
  1385. cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
  1386. if (!cb) {
  1387. /* Last resort when we are OOM */
  1388. if (fence)
  1389. dma_fence_wait(fence, false);
  1390. amdgpu_vm_prt_put(adev);
  1391. } else {
  1392. cb->adev = adev;
  1393. if (!fence || dma_fence_add_callback(fence, &cb->cb,
  1394. amdgpu_vm_prt_cb))
  1395. amdgpu_vm_prt_cb(fence, &cb->cb);
  1396. }
  1397. }
  1398. /**
  1399. * amdgpu_vm_free_mapping - free a mapping
  1400. *
  1401. * @adev: amdgpu_device pointer
  1402. * @vm: requested vm
  1403. * @mapping: mapping to be freed
  1404. * @fence: fence of the unmap operation
  1405. *
  1406. * Free a mapping and make sure we decrease the PRT usage count if applicable.
  1407. */
  1408. static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
  1409. struct amdgpu_vm *vm,
  1410. struct amdgpu_bo_va_mapping *mapping,
  1411. struct dma_fence *fence)
  1412. {
  1413. if (mapping->flags & AMDGPU_PTE_PRT)
  1414. amdgpu_vm_add_prt_cb(adev, fence);
  1415. kfree(mapping);
  1416. }
  1417. /**
  1418. * amdgpu_vm_prt_fini - finish all prt mappings
  1419. *
  1420. * @adev: amdgpu_device pointer
  1421. * @vm: requested vm
  1422. *
  1423. * Register a cleanup callback to disable PRT support after VM dies.
  1424. */
  1425. static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  1426. {
  1427. struct reservation_object *resv = vm->root.base.bo->tbo.resv;
  1428. struct dma_fence *excl, **shared;
  1429. unsigned i, shared_count;
  1430. int r;
  1431. r = reservation_object_get_fences_rcu(resv, &excl,
  1432. &shared_count, &shared);
  1433. if (r) {
  1434. /* Not enough memory to grab the fence list, as last resort
  1435. * block for all the fences to complete.
  1436. */
  1437. reservation_object_wait_timeout_rcu(resv, true, false,
  1438. MAX_SCHEDULE_TIMEOUT);
  1439. return;
  1440. }
  1441. /* Add a callback for each fence in the reservation object */
  1442. amdgpu_vm_prt_get(adev);
  1443. amdgpu_vm_add_prt_cb(adev, excl);
  1444. for (i = 0; i < shared_count; ++i) {
  1445. amdgpu_vm_prt_get(adev);
  1446. amdgpu_vm_add_prt_cb(adev, shared[i]);
  1447. }
  1448. kfree(shared);
  1449. }
  1450. /**
  1451. * amdgpu_vm_clear_freed - clear freed BOs in the PT
  1452. *
  1453. * @adev: amdgpu_device pointer
  1454. * @vm: requested vm
  1455. * @fence: optional resulting fence (unchanged if no work needed to be done
  1456. * or if an error occurred)
  1457. *
  1458. * Make sure all freed BOs are cleared in the PT.
  1459. * Returns 0 for success.
  1460. *
  1461. * PTs have to be reserved and mutex must be locked!
  1462. */
  1463. int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
  1464. struct amdgpu_vm *vm,
  1465. struct dma_fence **fence)
  1466. {
  1467. struct amdgpu_bo_va_mapping *mapping;
  1468. uint64_t init_pte_value = 0;
  1469. struct dma_fence *f = NULL;
  1470. int r;
  1471. while (!list_empty(&vm->freed)) {
  1472. mapping = list_first_entry(&vm->freed,
  1473. struct amdgpu_bo_va_mapping, list);
  1474. list_del(&mapping->list);
  1475. if (vm->pte_support_ats && mapping->start < AMDGPU_VA_HOLE_START)
  1476. init_pte_value = AMDGPU_PTE_DEFAULT_ATC;
  1477. r = amdgpu_vm_bo_update_mapping(adev, NULL, NULL, vm,
  1478. mapping->start, mapping->last,
  1479. init_pte_value, 0, &f);
  1480. amdgpu_vm_free_mapping(adev, vm, mapping, f);
  1481. if (r) {
  1482. dma_fence_put(f);
  1483. return r;
  1484. }
  1485. }
  1486. if (fence && f) {
  1487. dma_fence_put(*fence);
  1488. *fence = f;
  1489. } else {
  1490. dma_fence_put(f);
  1491. }
  1492. return 0;
  1493. }
  1494. /**
  1495. * amdgpu_vm_handle_moved - handle moved BOs in the PT
  1496. *
  1497. * @adev: amdgpu_device pointer
  1498. * @vm: requested vm
  1499. * @sync: sync object to add fences to
  1500. *
  1501. * Make sure all BOs which are moved are updated in the PTs.
  1502. * Returns 0 for success.
  1503. *
  1504. * PTs have to be reserved!
  1505. */
  1506. int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
  1507. struct amdgpu_vm *vm)
  1508. {
  1509. bool clear;
  1510. int r = 0;
  1511. spin_lock(&vm->status_lock);
  1512. while (!list_empty(&vm->moved)) {
  1513. struct amdgpu_bo_va *bo_va;
  1514. struct reservation_object *resv;
  1515. bo_va = list_first_entry(&vm->moved,
  1516. struct amdgpu_bo_va, base.vm_status);
  1517. spin_unlock(&vm->status_lock);
  1518. resv = bo_va->base.bo->tbo.resv;
  1519. /* Per VM BOs never need to bo cleared in the page tables */
  1520. if (resv == vm->root.base.bo->tbo.resv)
  1521. clear = false;
  1522. /* Try to reserve the BO to avoid clearing its ptes */
  1523. else if (!amdgpu_vm_debug && reservation_object_trylock(resv))
  1524. clear = false;
  1525. /* Somebody else is using the BO right now */
  1526. else
  1527. clear = true;
  1528. r = amdgpu_vm_bo_update(adev, bo_va, clear);
  1529. if (r)
  1530. return r;
  1531. if (!clear && resv != vm->root.base.bo->tbo.resv)
  1532. reservation_object_unlock(resv);
  1533. spin_lock(&vm->status_lock);
  1534. }
  1535. spin_unlock(&vm->status_lock);
  1536. return r;
  1537. }
  1538. /**
  1539. * amdgpu_vm_bo_add - add a bo to a specific vm
  1540. *
  1541. * @adev: amdgpu_device pointer
  1542. * @vm: requested vm
  1543. * @bo: amdgpu buffer object
  1544. *
  1545. * Add @bo into the requested vm.
  1546. * Add @bo to the list of bos associated with the vm
  1547. * Returns newly added bo_va or NULL for failure
  1548. *
  1549. * Object has to be reserved!
  1550. */
  1551. struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
  1552. struct amdgpu_vm *vm,
  1553. struct amdgpu_bo *bo)
  1554. {
  1555. struct amdgpu_bo_va *bo_va;
  1556. bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
  1557. if (bo_va == NULL) {
  1558. return NULL;
  1559. }
  1560. bo_va->base.vm = vm;
  1561. bo_va->base.bo = bo;
  1562. INIT_LIST_HEAD(&bo_va->base.bo_list);
  1563. INIT_LIST_HEAD(&bo_va->base.vm_status);
  1564. bo_va->ref_count = 1;
  1565. INIT_LIST_HEAD(&bo_va->valids);
  1566. INIT_LIST_HEAD(&bo_va->invalids);
  1567. if (!bo)
  1568. return bo_va;
  1569. list_add_tail(&bo_va->base.bo_list, &bo->va);
  1570. if (bo->tbo.resv != vm->root.base.bo->tbo.resv)
  1571. return bo_va;
  1572. if (bo->preferred_domains &
  1573. amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type))
  1574. return bo_va;
  1575. /*
  1576. * We checked all the prerequisites, but it looks like this per VM BO
  1577. * is currently evicted. add the BO to the evicted list to make sure it
  1578. * is validated on next VM use to avoid fault.
  1579. * */
  1580. spin_lock(&vm->status_lock);
  1581. list_move_tail(&bo_va->base.vm_status, &vm->evicted);
  1582. spin_unlock(&vm->status_lock);
  1583. return bo_va;
  1584. }
  1585. /**
  1586. * amdgpu_vm_bo_insert_mapping - insert a new mapping
  1587. *
  1588. * @adev: amdgpu_device pointer
  1589. * @bo_va: bo_va to store the address
  1590. * @mapping: the mapping to insert
  1591. *
  1592. * Insert a new mapping into all structures.
  1593. */
  1594. static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
  1595. struct amdgpu_bo_va *bo_va,
  1596. struct amdgpu_bo_va_mapping *mapping)
  1597. {
  1598. struct amdgpu_vm *vm = bo_va->base.vm;
  1599. struct amdgpu_bo *bo = bo_va->base.bo;
  1600. mapping->bo_va = bo_va;
  1601. list_add(&mapping->list, &bo_va->invalids);
  1602. amdgpu_vm_it_insert(mapping, &vm->va);
  1603. if (mapping->flags & AMDGPU_PTE_PRT)
  1604. amdgpu_vm_prt_get(adev);
  1605. if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
  1606. spin_lock(&vm->status_lock);
  1607. if (list_empty(&bo_va->base.vm_status))
  1608. list_add(&bo_va->base.vm_status, &vm->moved);
  1609. spin_unlock(&vm->status_lock);
  1610. }
  1611. trace_amdgpu_vm_bo_map(bo_va, mapping);
  1612. }
  1613. /**
  1614. * amdgpu_vm_bo_map - map bo inside a vm
  1615. *
  1616. * @adev: amdgpu_device pointer
  1617. * @bo_va: bo_va to store the address
  1618. * @saddr: where to map the BO
  1619. * @offset: requested offset in the BO
  1620. * @flags: attributes of pages (read/write/valid/etc.)
  1621. *
  1622. * Add a mapping of the BO at the specefied addr into the VM.
  1623. * Returns 0 for success, error for failure.
  1624. *
  1625. * Object has to be reserved and unreserved outside!
  1626. */
  1627. int amdgpu_vm_bo_map(struct amdgpu_device *adev,
  1628. struct amdgpu_bo_va *bo_va,
  1629. uint64_t saddr, uint64_t offset,
  1630. uint64_t size, uint64_t flags)
  1631. {
  1632. struct amdgpu_bo_va_mapping *mapping, *tmp;
  1633. struct amdgpu_bo *bo = bo_va->base.bo;
  1634. struct amdgpu_vm *vm = bo_va->base.vm;
  1635. uint64_t eaddr;
  1636. /* validate the parameters */
  1637. if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
  1638. size == 0 || size & AMDGPU_GPU_PAGE_MASK)
  1639. return -EINVAL;
  1640. /* make sure object fit at this offset */
  1641. eaddr = saddr + size - 1;
  1642. if (saddr >= eaddr ||
  1643. (bo && offset + size > amdgpu_bo_size(bo)))
  1644. return -EINVAL;
  1645. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1646. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  1647. tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
  1648. if (tmp) {
  1649. /* bo and tmp overlap, invalid addr */
  1650. dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
  1651. "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr,
  1652. tmp->start, tmp->last + 1);
  1653. return -EINVAL;
  1654. }
  1655. mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
  1656. if (!mapping)
  1657. return -ENOMEM;
  1658. mapping->start = saddr;
  1659. mapping->last = eaddr;
  1660. mapping->offset = offset;
  1661. mapping->flags = flags;
  1662. amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
  1663. return 0;
  1664. }
  1665. /**
  1666. * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
  1667. *
  1668. * @adev: amdgpu_device pointer
  1669. * @bo_va: bo_va to store the address
  1670. * @saddr: where to map the BO
  1671. * @offset: requested offset in the BO
  1672. * @flags: attributes of pages (read/write/valid/etc.)
  1673. *
  1674. * Add a mapping of the BO at the specefied addr into the VM. Replace existing
  1675. * mappings as we do so.
  1676. * Returns 0 for success, error for failure.
  1677. *
  1678. * Object has to be reserved and unreserved outside!
  1679. */
  1680. int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
  1681. struct amdgpu_bo_va *bo_va,
  1682. uint64_t saddr, uint64_t offset,
  1683. uint64_t size, uint64_t flags)
  1684. {
  1685. struct amdgpu_bo_va_mapping *mapping;
  1686. struct amdgpu_bo *bo = bo_va->base.bo;
  1687. uint64_t eaddr;
  1688. int r;
  1689. /* validate the parameters */
  1690. if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
  1691. size == 0 || size & AMDGPU_GPU_PAGE_MASK)
  1692. return -EINVAL;
  1693. /* make sure object fit at this offset */
  1694. eaddr = saddr + size - 1;
  1695. if (saddr >= eaddr ||
  1696. (bo && offset + size > amdgpu_bo_size(bo)))
  1697. return -EINVAL;
  1698. /* Allocate all the needed memory */
  1699. mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
  1700. if (!mapping)
  1701. return -ENOMEM;
  1702. r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size);
  1703. if (r) {
  1704. kfree(mapping);
  1705. return r;
  1706. }
  1707. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1708. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  1709. mapping->start = saddr;
  1710. mapping->last = eaddr;
  1711. mapping->offset = offset;
  1712. mapping->flags = flags;
  1713. amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
  1714. return 0;
  1715. }
  1716. /**
  1717. * amdgpu_vm_bo_unmap - remove bo mapping from vm
  1718. *
  1719. * @adev: amdgpu_device pointer
  1720. * @bo_va: bo_va to remove the address from
  1721. * @saddr: where to the BO is mapped
  1722. *
  1723. * Remove a mapping of the BO at the specefied addr from the VM.
  1724. * Returns 0 for success, error for failure.
  1725. *
  1726. * Object has to be reserved and unreserved outside!
  1727. */
  1728. int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
  1729. struct amdgpu_bo_va *bo_va,
  1730. uint64_t saddr)
  1731. {
  1732. struct amdgpu_bo_va_mapping *mapping;
  1733. struct amdgpu_vm *vm = bo_va->base.vm;
  1734. bool valid = true;
  1735. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1736. list_for_each_entry(mapping, &bo_va->valids, list) {
  1737. if (mapping->start == saddr)
  1738. break;
  1739. }
  1740. if (&mapping->list == &bo_va->valids) {
  1741. valid = false;
  1742. list_for_each_entry(mapping, &bo_va->invalids, list) {
  1743. if (mapping->start == saddr)
  1744. break;
  1745. }
  1746. if (&mapping->list == &bo_va->invalids)
  1747. return -ENOENT;
  1748. }
  1749. list_del(&mapping->list);
  1750. amdgpu_vm_it_remove(mapping, &vm->va);
  1751. mapping->bo_va = NULL;
  1752. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  1753. if (valid)
  1754. list_add(&mapping->list, &vm->freed);
  1755. else
  1756. amdgpu_vm_free_mapping(adev, vm, mapping,
  1757. bo_va->last_pt_update);
  1758. return 0;
  1759. }
  1760. /**
  1761. * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
  1762. *
  1763. * @adev: amdgpu_device pointer
  1764. * @vm: VM structure to use
  1765. * @saddr: start of the range
  1766. * @size: size of the range
  1767. *
  1768. * Remove all mappings in a range, split them as appropriate.
  1769. * Returns 0 for success, error for failure.
  1770. */
  1771. int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
  1772. struct amdgpu_vm *vm,
  1773. uint64_t saddr, uint64_t size)
  1774. {
  1775. struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
  1776. LIST_HEAD(removed);
  1777. uint64_t eaddr;
  1778. eaddr = saddr + size - 1;
  1779. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1780. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  1781. /* Allocate all the needed memory */
  1782. before = kzalloc(sizeof(*before), GFP_KERNEL);
  1783. if (!before)
  1784. return -ENOMEM;
  1785. INIT_LIST_HEAD(&before->list);
  1786. after = kzalloc(sizeof(*after), GFP_KERNEL);
  1787. if (!after) {
  1788. kfree(before);
  1789. return -ENOMEM;
  1790. }
  1791. INIT_LIST_HEAD(&after->list);
  1792. /* Now gather all removed mappings */
  1793. tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
  1794. while (tmp) {
  1795. /* Remember mapping split at the start */
  1796. if (tmp->start < saddr) {
  1797. before->start = tmp->start;
  1798. before->last = saddr - 1;
  1799. before->offset = tmp->offset;
  1800. before->flags = tmp->flags;
  1801. list_add(&before->list, &tmp->list);
  1802. }
  1803. /* Remember mapping split at the end */
  1804. if (tmp->last > eaddr) {
  1805. after->start = eaddr + 1;
  1806. after->last = tmp->last;
  1807. after->offset = tmp->offset;
  1808. after->offset += after->start - tmp->start;
  1809. after->flags = tmp->flags;
  1810. list_add(&after->list, &tmp->list);
  1811. }
  1812. list_del(&tmp->list);
  1813. list_add(&tmp->list, &removed);
  1814. tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
  1815. }
  1816. /* And free them up */
  1817. list_for_each_entry_safe(tmp, next, &removed, list) {
  1818. amdgpu_vm_it_remove(tmp, &vm->va);
  1819. list_del(&tmp->list);
  1820. if (tmp->start < saddr)
  1821. tmp->start = saddr;
  1822. if (tmp->last > eaddr)
  1823. tmp->last = eaddr;
  1824. tmp->bo_va = NULL;
  1825. list_add(&tmp->list, &vm->freed);
  1826. trace_amdgpu_vm_bo_unmap(NULL, tmp);
  1827. }
  1828. /* Insert partial mapping before the range */
  1829. if (!list_empty(&before->list)) {
  1830. amdgpu_vm_it_insert(before, &vm->va);
  1831. if (before->flags & AMDGPU_PTE_PRT)
  1832. amdgpu_vm_prt_get(adev);
  1833. } else {
  1834. kfree(before);
  1835. }
  1836. /* Insert partial mapping after the range */
  1837. if (!list_empty(&after->list)) {
  1838. amdgpu_vm_it_insert(after, &vm->va);
  1839. if (after->flags & AMDGPU_PTE_PRT)
  1840. amdgpu_vm_prt_get(adev);
  1841. } else {
  1842. kfree(after);
  1843. }
  1844. return 0;
  1845. }
  1846. /**
  1847. * amdgpu_vm_bo_lookup_mapping - find mapping by address
  1848. *
  1849. * @vm: the requested VM
  1850. *
  1851. * Find a mapping by it's address.
  1852. */
  1853. struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
  1854. uint64_t addr)
  1855. {
  1856. return amdgpu_vm_it_iter_first(&vm->va, addr, addr);
  1857. }
  1858. /**
  1859. * amdgpu_vm_bo_rmv - remove a bo to a specific vm
  1860. *
  1861. * @adev: amdgpu_device pointer
  1862. * @bo_va: requested bo_va
  1863. *
  1864. * Remove @bo_va->bo from the requested vm.
  1865. *
  1866. * Object have to be reserved!
  1867. */
  1868. void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
  1869. struct amdgpu_bo_va *bo_va)
  1870. {
  1871. struct amdgpu_bo_va_mapping *mapping, *next;
  1872. struct amdgpu_vm *vm = bo_va->base.vm;
  1873. list_del(&bo_va->base.bo_list);
  1874. spin_lock(&vm->status_lock);
  1875. list_del(&bo_va->base.vm_status);
  1876. spin_unlock(&vm->status_lock);
  1877. list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
  1878. list_del(&mapping->list);
  1879. amdgpu_vm_it_remove(mapping, &vm->va);
  1880. mapping->bo_va = NULL;
  1881. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  1882. list_add(&mapping->list, &vm->freed);
  1883. }
  1884. list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
  1885. list_del(&mapping->list);
  1886. amdgpu_vm_it_remove(mapping, &vm->va);
  1887. amdgpu_vm_free_mapping(adev, vm, mapping,
  1888. bo_va->last_pt_update);
  1889. }
  1890. dma_fence_put(bo_va->last_pt_update);
  1891. kfree(bo_va);
  1892. }
  1893. /**
  1894. * amdgpu_vm_bo_invalidate - mark the bo as invalid
  1895. *
  1896. * @adev: amdgpu_device pointer
  1897. * @vm: requested vm
  1898. * @bo: amdgpu buffer object
  1899. *
  1900. * Mark @bo as invalid.
  1901. */
  1902. void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
  1903. struct amdgpu_bo *bo, bool evicted)
  1904. {
  1905. struct amdgpu_vm_bo_base *bo_base;
  1906. list_for_each_entry(bo_base, &bo->va, bo_list) {
  1907. struct amdgpu_vm *vm = bo_base->vm;
  1908. bo_base->moved = true;
  1909. if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
  1910. spin_lock(&bo_base->vm->status_lock);
  1911. if (bo->tbo.type == ttm_bo_type_kernel)
  1912. list_move(&bo_base->vm_status, &vm->evicted);
  1913. else
  1914. list_move_tail(&bo_base->vm_status,
  1915. &vm->evicted);
  1916. spin_unlock(&bo_base->vm->status_lock);
  1917. continue;
  1918. }
  1919. if (bo->tbo.type == ttm_bo_type_kernel) {
  1920. spin_lock(&bo_base->vm->status_lock);
  1921. if (list_empty(&bo_base->vm_status))
  1922. list_add(&bo_base->vm_status, &vm->relocated);
  1923. spin_unlock(&bo_base->vm->status_lock);
  1924. continue;
  1925. }
  1926. spin_lock(&bo_base->vm->status_lock);
  1927. if (list_empty(&bo_base->vm_status))
  1928. list_add(&bo_base->vm_status, &vm->moved);
  1929. spin_unlock(&bo_base->vm->status_lock);
  1930. }
  1931. }
  1932. static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
  1933. {
  1934. /* Total bits covered by PD + PTs */
  1935. unsigned bits = ilog2(vm_size) + 18;
  1936. /* Make sure the PD is 4K in size up to 8GB address space.
  1937. Above that split equal between PD and PTs */
  1938. if (vm_size <= 8)
  1939. return (bits - 9);
  1940. else
  1941. return ((bits + 3) / 2);
  1942. }
  1943. /**
  1944. * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size
  1945. *
  1946. * @adev: amdgpu_device pointer
  1947. * @vm_size: the default vm size if it's set auto
  1948. */
  1949. void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t vm_size,
  1950. uint32_t fragment_size_default, unsigned max_level,
  1951. unsigned max_bits)
  1952. {
  1953. uint64_t tmp;
  1954. /* adjust vm size first */
  1955. if (amdgpu_vm_size != -1) {
  1956. unsigned max_size = 1 << (max_bits - 30);
  1957. vm_size = amdgpu_vm_size;
  1958. if (vm_size > max_size) {
  1959. dev_warn(adev->dev, "VM size (%d) too large, max is %u GB\n",
  1960. amdgpu_vm_size, max_size);
  1961. vm_size = max_size;
  1962. }
  1963. }
  1964. adev->vm_manager.max_pfn = (uint64_t)vm_size << 18;
  1965. tmp = roundup_pow_of_two(adev->vm_manager.max_pfn);
  1966. if (amdgpu_vm_block_size != -1)
  1967. tmp >>= amdgpu_vm_block_size - 9;
  1968. tmp = DIV_ROUND_UP(fls64(tmp) - 1, 9) - 1;
  1969. adev->vm_manager.num_level = min(max_level, (unsigned)tmp);
  1970. switch (adev->vm_manager.num_level) {
  1971. case 3:
  1972. adev->vm_manager.root_level = AMDGPU_VM_PDB2;
  1973. break;
  1974. case 2:
  1975. adev->vm_manager.root_level = AMDGPU_VM_PDB1;
  1976. break;
  1977. case 1:
  1978. adev->vm_manager.root_level = AMDGPU_VM_PDB0;
  1979. break;
  1980. default:
  1981. dev_err(adev->dev, "VMPT only supports 2~4+1 levels\n");
  1982. }
  1983. /* block size depends on vm size and hw setup*/
  1984. if (amdgpu_vm_block_size != -1)
  1985. adev->vm_manager.block_size =
  1986. min((unsigned)amdgpu_vm_block_size, max_bits
  1987. - AMDGPU_GPU_PAGE_SHIFT
  1988. - 9 * adev->vm_manager.num_level);
  1989. else if (adev->vm_manager.num_level > 1)
  1990. adev->vm_manager.block_size = 9;
  1991. else
  1992. adev->vm_manager.block_size = amdgpu_vm_get_block_size(tmp);
  1993. if (amdgpu_vm_fragment_size == -1)
  1994. adev->vm_manager.fragment_size = fragment_size_default;
  1995. else
  1996. adev->vm_manager.fragment_size = amdgpu_vm_fragment_size;
  1997. DRM_INFO("vm size is %u GB, %u levels, block size is %u-bit, fragment size is %u-bit\n",
  1998. vm_size, adev->vm_manager.num_level + 1,
  1999. adev->vm_manager.block_size,
  2000. adev->vm_manager.fragment_size);
  2001. }
  2002. /**
  2003. * amdgpu_vm_init - initialize a vm instance
  2004. *
  2005. * @adev: amdgpu_device pointer
  2006. * @vm: requested vm
  2007. * @vm_context: Indicates if it GFX or Compute context
  2008. *
  2009. * Init @vm fields.
  2010. */
  2011. int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  2012. int vm_context, unsigned int pasid)
  2013. {
  2014. const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
  2015. AMDGPU_VM_PTE_COUNT(adev) * 8);
  2016. unsigned ring_instance;
  2017. struct amdgpu_ring *ring;
  2018. struct drm_sched_rq *rq;
  2019. unsigned long size;
  2020. uint64_t flags;
  2021. int r, i;
  2022. vm->va = RB_ROOT_CACHED;
  2023. for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
  2024. vm->reserved_vmid[i] = NULL;
  2025. spin_lock_init(&vm->status_lock);
  2026. INIT_LIST_HEAD(&vm->evicted);
  2027. INIT_LIST_HEAD(&vm->relocated);
  2028. INIT_LIST_HEAD(&vm->moved);
  2029. INIT_LIST_HEAD(&vm->freed);
  2030. /* create scheduler entity for page table updates */
  2031. ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
  2032. ring_instance %= adev->vm_manager.vm_pte_num_rings;
  2033. ring = adev->vm_manager.vm_pte_rings[ring_instance];
  2034. rq = &ring->sched.sched_rq[DRM_SCHED_PRIORITY_KERNEL];
  2035. r = drm_sched_entity_init(&ring->sched, &vm->entity,
  2036. rq, amdgpu_sched_jobs, NULL);
  2037. if (r)
  2038. return r;
  2039. vm->pte_support_ats = false;
  2040. if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) {
  2041. vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
  2042. AMDGPU_VM_USE_CPU_FOR_COMPUTE);
  2043. if (adev->asic_type == CHIP_RAVEN)
  2044. vm->pte_support_ats = true;
  2045. } else {
  2046. vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
  2047. AMDGPU_VM_USE_CPU_FOR_GFX);
  2048. }
  2049. DRM_DEBUG_DRIVER("VM update mode is %s\n",
  2050. vm->use_cpu_for_update ? "CPU" : "SDMA");
  2051. WARN_ONCE((vm->use_cpu_for_update & !amdgpu_vm_is_large_bar(adev)),
  2052. "CPU update of VM recommended only for large BAR system\n");
  2053. vm->last_update = NULL;
  2054. flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
  2055. if (vm->use_cpu_for_update)
  2056. flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
  2057. else
  2058. flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
  2059. AMDGPU_GEM_CREATE_SHADOW);
  2060. size = amdgpu_vm_bo_size(adev, adev->vm_manager.root_level);
  2061. r = amdgpu_bo_create(adev, size, align, true, AMDGPU_GEM_DOMAIN_VRAM,
  2062. flags, NULL, NULL, &vm->root.base.bo);
  2063. if (r)
  2064. goto error_free_sched_entity;
  2065. r = amdgpu_bo_reserve(vm->root.base.bo, true);
  2066. if (r)
  2067. goto error_free_root;
  2068. r = amdgpu_vm_clear_bo(adev, vm, vm->root.base.bo,
  2069. adev->vm_manager.root_level,
  2070. vm->pte_support_ats);
  2071. if (r)
  2072. goto error_unreserve;
  2073. vm->root.base.vm = vm;
  2074. list_add_tail(&vm->root.base.bo_list, &vm->root.base.bo->va);
  2075. list_add_tail(&vm->root.base.vm_status, &vm->evicted);
  2076. amdgpu_bo_unreserve(vm->root.base.bo);
  2077. if (pasid) {
  2078. unsigned long flags;
  2079. spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
  2080. r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1,
  2081. GFP_ATOMIC);
  2082. spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
  2083. if (r < 0)
  2084. goto error_free_root;
  2085. vm->pasid = pasid;
  2086. }
  2087. INIT_KFIFO(vm->faults);
  2088. vm->fault_credit = 16;
  2089. return 0;
  2090. error_unreserve:
  2091. amdgpu_bo_unreserve(vm->root.base.bo);
  2092. error_free_root:
  2093. amdgpu_bo_unref(&vm->root.base.bo->shadow);
  2094. amdgpu_bo_unref(&vm->root.base.bo);
  2095. vm->root.base.bo = NULL;
  2096. error_free_sched_entity:
  2097. drm_sched_entity_fini(&ring->sched, &vm->entity);
  2098. return r;
  2099. }
  2100. /**
  2101. * amdgpu_vm_free_levels - free PD/PT levels
  2102. *
  2103. * @adev: amdgpu device structure
  2104. * @parent: PD/PT starting level to free
  2105. * @level: level of parent structure
  2106. *
  2107. * Free the page directory or page table level and all sub levels.
  2108. */
  2109. static void amdgpu_vm_free_levels(struct amdgpu_device *adev,
  2110. struct amdgpu_vm_pt *parent,
  2111. unsigned level)
  2112. {
  2113. unsigned i, num_entries = amdgpu_vm_num_entries(adev, level);
  2114. if (parent->base.bo) {
  2115. list_del(&parent->base.bo_list);
  2116. list_del(&parent->base.vm_status);
  2117. amdgpu_bo_unref(&parent->base.bo->shadow);
  2118. amdgpu_bo_unref(&parent->base.bo);
  2119. }
  2120. if (parent->entries)
  2121. for (i = 0; i < num_entries; i++)
  2122. amdgpu_vm_free_levels(adev, &parent->entries[i],
  2123. level + 1);
  2124. kvfree(parent->entries);
  2125. }
  2126. /**
  2127. * amdgpu_vm_fini - tear down a vm instance
  2128. *
  2129. * @adev: amdgpu_device pointer
  2130. * @vm: requested vm
  2131. *
  2132. * Tear down @vm.
  2133. * Unbind the VM and remove all bos from the vm bo list
  2134. */
  2135. void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  2136. {
  2137. struct amdgpu_bo_va_mapping *mapping, *tmp;
  2138. bool prt_fini_needed = !!adev->gmc.gmc_funcs->set_prt;
  2139. struct amdgpu_bo *root;
  2140. u64 fault;
  2141. int i, r;
  2142. /* Clear pending page faults from IH when the VM is destroyed */
  2143. while (kfifo_get(&vm->faults, &fault))
  2144. amdgpu_ih_clear_fault(adev, fault);
  2145. if (vm->pasid) {
  2146. unsigned long flags;
  2147. spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
  2148. idr_remove(&adev->vm_manager.pasid_idr, vm->pasid);
  2149. spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
  2150. }
  2151. drm_sched_entity_fini(vm->entity.sched, &vm->entity);
  2152. if (!RB_EMPTY_ROOT(&vm->va.rb_root)) {
  2153. dev_err(adev->dev, "still active bo inside vm\n");
  2154. }
  2155. rbtree_postorder_for_each_entry_safe(mapping, tmp,
  2156. &vm->va.rb_root, rb) {
  2157. list_del(&mapping->list);
  2158. amdgpu_vm_it_remove(mapping, &vm->va);
  2159. kfree(mapping);
  2160. }
  2161. list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
  2162. if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
  2163. amdgpu_vm_prt_fini(adev, vm);
  2164. prt_fini_needed = false;
  2165. }
  2166. list_del(&mapping->list);
  2167. amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
  2168. }
  2169. root = amdgpu_bo_ref(vm->root.base.bo);
  2170. r = amdgpu_bo_reserve(root, true);
  2171. if (r) {
  2172. dev_err(adev->dev, "Leaking page tables because BO reservation failed\n");
  2173. } else {
  2174. amdgpu_vm_free_levels(adev, &vm->root,
  2175. adev->vm_manager.root_level);
  2176. amdgpu_bo_unreserve(root);
  2177. }
  2178. amdgpu_bo_unref(&root);
  2179. dma_fence_put(vm->last_update);
  2180. for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
  2181. amdgpu_vmid_free_reserved(adev, vm, i);
  2182. }
  2183. /**
  2184. * amdgpu_vm_pasid_fault_credit - Check fault credit for given PASID
  2185. *
  2186. * @adev: amdgpu_device pointer
  2187. * @pasid: PASID do identify the VM
  2188. *
  2189. * This function is expected to be called in interrupt context. Returns
  2190. * true if there was fault credit, false otherwise
  2191. */
  2192. bool amdgpu_vm_pasid_fault_credit(struct amdgpu_device *adev,
  2193. unsigned int pasid)
  2194. {
  2195. struct amdgpu_vm *vm;
  2196. spin_lock(&adev->vm_manager.pasid_lock);
  2197. vm = idr_find(&adev->vm_manager.pasid_idr, pasid);
  2198. if (!vm) {
  2199. /* VM not found, can't track fault credit */
  2200. spin_unlock(&adev->vm_manager.pasid_lock);
  2201. return true;
  2202. }
  2203. /* No lock needed. only accessed by IRQ handler */
  2204. if (!vm->fault_credit) {
  2205. /* Too many faults in this VM */
  2206. spin_unlock(&adev->vm_manager.pasid_lock);
  2207. return false;
  2208. }
  2209. vm->fault_credit--;
  2210. spin_unlock(&adev->vm_manager.pasid_lock);
  2211. return true;
  2212. }
  2213. /**
  2214. * amdgpu_vm_manager_init - init the VM manager
  2215. *
  2216. * @adev: amdgpu_device pointer
  2217. *
  2218. * Initialize the VM manager structures
  2219. */
  2220. void amdgpu_vm_manager_init(struct amdgpu_device *adev)
  2221. {
  2222. unsigned i;
  2223. amdgpu_vmid_mgr_init(adev);
  2224. adev->vm_manager.fence_context =
  2225. dma_fence_context_alloc(AMDGPU_MAX_RINGS);
  2226. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  2227. adev->vm_manager.seqno[i] = 0;
  2228. atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
  2229. spin_lock_init(&adev->vm_manager.prt_lock);
  2230. atomic_set(&adev->vm_manager.num_prt_users, 0);
  2231. /* If not overridden by the user, by default, only in large BAR systems
  2232. * Compute VM tables will be updated by CPU
  2233. */
  2234. #ifdef CONFIG_X86_64
  2235. if (amdgpu_vm_update_mode == -1) {
  2236. if (amdgpu_vm_is_large_bar(adev))
  2237. adev->vm_manager.vm_update_mode =
  2238. AMDGPU_VM_USE_CPU_FOR_COMPUTE;
  2239. else
  2240. adev->vm_manager.vm_update_mode = 0;
  2241. } else
  2242. adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
  2243. #else
  2244. adev->vm_manager.vm_update_mode = 0;
  2245. #endif
  2246. idr_init(&adev->vm_manager.pasid_idr);
  2247. spin_lock_init(&adev->vm_manager.pasid_lock);
  2248. }
  2249. /**
  2250. * amdgpu_vm_manager_fini - cleanup VM manager
  2251. *
  2252. * @adev: amdgpu_device pointer
  2253. *
  2254. * Cleanup the VM manager and free resources.
  2255. */
  2256. void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
  2257. {
  2258. WARN_ON(!idr_is_empty(&adev->vm_manager.pasid_idr));
  2259. idr_destroy(&adev->vm_manager.pasid_idr);
  2260. amdgpu_vmid_mgr_fini(adev);
  2261. }
  2262. int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  2263. {
  2264. union drm_amdgpu_vm *args = data;
  2265. struct amdgpu_device *adev = dev->dev_private;
  2266. struct amdgpu_fpriv *fpriv = filp->driver_priv;
  2267. int r;
  2268. switch (args->in.op) {
  2269. case AMDGPU_VM_OP_RESERVE_VMID:
  2270. /* current, we only have requirement to reserve vmid from gfxhub */
  2271. r = amdgpu_vmid_alloc_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB);
  2272. if (r)
  2273. return r;
  2274. break;
  2275. case AMDGPU_VM_OP_UNRESERVE_VMID:
  2276. amdgpu_vmid_free_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB);
  2277. break;
  2278. default:
  2279. return -EINVAL;
  2280. }
  2281. return 0;
  2282. }