amdgpu_vm.c 67 KB

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