amdgpu_vm.c 52 KB

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