amdgpu_vm.c 76 KB

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