amdgpu_vm.c 51 KB

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