amdgpu_vm.c 76 KB

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