amdgpu_vm.c 73 KB

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