amdgpu_vm.c 63 KB

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