amdgpu_vm.c 67 KB

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