amdgpu_vm.c 80 KB

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