amdgpu_vm.c 56 KB

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