amdgpu_vm.c 77 KB

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