amdgpu_vm.c 77 KB

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