amdgpu_vm.c 71 KB

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