amdgpu_vm.c 70 KB

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