amdgpu_vm.c 58 KB

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