amdgpu_vm.c 70 KB

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