amdgpu_vm.c 72 KB

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