amdgpu_vm.c 76 KB

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