amdgpu_vm.c 87 KB

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