amdgpu_vm.c 70 KB

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