amdgpu_vm.c 76 KB

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