amdgpu_vm.c 68 KB

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