amdgpu_vm.c 71 KB

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