amdgpu_vm.c 71 KB

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