amdgpu_vm.c 78 KB

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