amdgpu_vm.c 77 KB

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