amdgpu_vm.c 79 KB

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