amdgpu_vm.c 76 KB

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