amdgpu_vm.c 70 KB

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