amdgpu_vm.c 73 KB

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