amdgpu_vm.c 72 KB

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