amdgpu_vm.c 73 KB

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