amdgpu_vm.c 69 KB

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