amdgpu_vm.c 67 KB

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