amdgpu_vm.c 71 KB

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