amdgpu_vm.c 70 KB

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