amdgpu_vm.c 77 KB

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